为什么这个POST不起作用?

时间:2011-02-01 21:57:17

标签: iphone objective-c nsurlconnection http-post nsmutableurlrequest


这是我的片段:

NSString *post = @"from=A&name=B&email=ciccio@aaasd.com&messaggio=MESSAGE&codHidden=-1";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];    

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.mysite.com/page.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData]; 

NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"Succeeded! Received %d bytes of data",[urlData length]);
NSString *outputdata = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSLog(@"%@", outputdata);

它调用一个向我发送电子邮件的php页面......

此代码中是否有任何错误?
似乎不起作用......

感谢

2 个答案:

答案 0 :(得分:0)

尝试将NSASCIIStringEncoding更改为NSUTF8StringEncoding

答案 1 :(得分:0)

是同步/异步问题吗?如果是这样,请使用委托模式并异步访问数据。你能发布你看到的任何错误(或服务器的响应字符串)吗?

编辑:由于您没有对返回的错误执行任何有用的操作,请更改您的类以异步方式执行请求 。之前发布了一个很好的答案here。实施didFailWithError后,您会有更好的了解。

相关问题