Objective c 无法使用NSURLConnection使iOS http post正常工作
我正在试着让一条帖子起作用。我已经看到了一些描述这样做的帖子,比如,但是我仍然无法让它工作。下面是我的objective-c代码:Objective c 无法使用NSURLConnection使iOS http post正常工作,objective-c,ios,post,nsurlrequest,Objective C,Ios,Post,Nsurlrequest,我正在试着让一条帖子起作用。我已经看到了一些描述这样做的帖子,比如,但是我仍然无法让它工作。下面是我的objective-c代码: NSString * urlString = @"http://magicthegatheringdatabase.com/test.php"; NSURL *aUrl = [NSURL URLWithString: urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWith
NSString * urlString = @"http://magicthegatheringdatabase.com/test.php";
NSURL *aUrl = [NSURL URLWithString: urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSString *postString = [NSString stringWithFormat:@"tes1=%@&test2=%@",
[@"hello" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[@"world" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData * postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody: postBody];
[request addValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *postLength = [NSString stringWithFormat:@"%d", postBody.length];
[request addValue: postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
NSError * error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request
returningResponse: nil
error: &error];
NSLog(@"%p, %@", error, error.localizedDescription);
NSString * returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Result: %@", returnString);
这是我的PHP:
<?php
print_r($_REQUEST);
print_r($_POST);
print_r($_GET);
echo file_get_contents( 'php://input' );
?>
所以我知道我的PHP正确地记录了get/请求,但我不确定为什么我通过objective-c代码发布的帖子不起作用。我很确定我忽略了一些愚蠢的事情。对我遗漏的内容有什么建议吗?更改:
NSString *postString = [NSString stringWithFormat:@"tes1=%@&test2=%@",
[@"hello" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[@"world" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
现在加上
[request setHTTPMethod:@"POST"];
您应该尝试添加以下内容:
[请求addValue:@“应用程序/x-www-form-urlencoded;字符集=utf-8”用于HttpHeaderField:@“内容类型”];
这是因为NSURLConnection
无法正确处理重定向
当您将POST
请求发送到URLhttp://magicthegatheringdatabase.com/test.php
,服务器将响应永久移动的301
,并重定向到http://www.magicthegatheringdatabase.com/test.php
与向新URL发送相同的请求不同,NSURLConnection
创建一个新的GET
请求,并丢弃原始请求的HTTPBody
。此行为是由于在未经用户确认的情况下,禁止自动重定向除HEAD
和GET
以外的请求。将方法更改为GET
是错误的,但至少它是一种安全的方法,不会损害HTTP资源
这个问题可以通过两个选项来解决:
http://www.magicthegatheringdatabase.com/test.php
NSURLConnection
实例,并实现连接:willSendRequest:redirectResponse:
,以便按照的回答中所述正确处理重定向。即使重定向发生更改,这也会起作用,但需要您编写更多代码,因为您需要等待响应,创建NSMutableData
对象,在出现字节时追加字节,并异步处理所有内容(请参阅)。但是,异步执行任务始终是一个好主意:如果需要,可以取消它们,并且不必冒长时间阻塞线程的风险(当您在主线程中时,任何超过1秒的时间都是很长的)尝试设置内容类型和大小。请执行另一项操作,而不是将nil设置为error,传递NSError的一些实例,这样您就可以知道将出现什么错误。希望,它能解决你的问题。顺便说一句,你想听的。@moonwave99顺便说一句,你想尽可能避免使用RestKit。那个框架是个怪物。谢谢大家。我尝试添加内容类型和内容大小(用细节更新了我的问题),但仍然没有成功。此外,我还包括错误详细信息。未检测到错误。感谢您的回答和详细信息。我知道我必须忽略一些简单的事情,这就做到了。
NSString *postString = [NSString stringWithFormat:@"tes1=%@&test2=%@",
[@"hello" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[@"world" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];