【问题标题】:NSURLRequest cannot handle HTTP body when method is not POST?当方法不是 POST 时,NSURLRequest 无法处理 HTTP 正文?
【发布时间】:2010-08-12 14:58:17
【问题描述】:

每当我在可变请求上设置主体并且方法设置为 POST 以外的任何内容时,主体都不会包含在请求中,并且当服务器回复时我会收到 kCFErrorDomainCFNetwork 错误 303 (kCFErrorHTTPParseFailure)。将方法更改为 POST 即可使请求顺利通过。有没有办法将主体附加到其他方法,或者我们是否对所有内容都坚持使用 POST?

这是提交代码:

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:assembleURL]];// cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:45.0];

#if (SERVER_TARGET_ARGS_ALLOWED==1)
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];

[req setHTTPMethod:ServerMessageMethods[operation]];    //value is @"POST" or other method name

#endif  

//run the payload into a JSON
SBJsonWriter *json = [[SBJsonWriter alloc] init];
NSString *encodedPayload = [json stringWithObject:payload];
encodedPayload = [NSString stringWithFormat:@"%@", [encodedPayload stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *dataPayload = [encodedPayload dataUsingEncoding:NSUTF8StringEncoding];
[req setHTTPBody:dataPayload];

NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];

【问题讨论】:

标签: iphone cocoa nsurlrequest


【解决方案1】:

我尝试查找有关它的更多最新信息,但有一篇关于您所谈论的确切问题的旧帖子:http://lists.apple.com/archives/cocoa-dev/2004/May/msg01847.html

基本上,他们提到这是代码中的一个错误。可悲的是,我希望我能找到更新的东西来证实这一点。

我一直使用的代码是ASIHTTPRequest,它绝对可以执行 PUT 请求,因为它们使用较低级别的代码集来创建 HTTP 消息并且不依赖于 NSMutableUrlRequest

另外,我发现另一篇博客文章讨论了这个问题以及您需要为 PUT 请求添加什么内容。 http://iphonedevelopment.blogspot.com/2008/06/http-put-and-nsmutableurlrequest.html

博文:

使用 NSMutableURLRequest 做 HTTP PUT 请求时,添加如下代码行(req 为 NSMutableURLRequest):

[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

仅此而已。如果你添加这行代码,你的 PUT 请求就可以正常工作了。

【讨论】:

  • 2016年依然存在
【解决方案2】:

您还记得设置Content-Length/Transfer-EncodingContent-Type 标头吗?我不认为Content-Length 会自动为您输入,这意味着服务器将假定它为 0。

更多信息:http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

【讨论】:

  • 只是想知道如何解释它适用于 POST 但不适用于其他操作?您认为它可能是针对一种操作而不是另一种操作设置的?
  • @christophercotton 它可能会自动设置,但我不知道;但是,未定义的Content-Length 可能会解释为什么服务器从不读取请求的正文。
  • 如果您使用 HTTP/1.1 的分块编码,则不需要 Content-Length。 NSURLConnection 应该为你透明地处理所有这些。
【解决方案3】:

在这里

+(NSString*)simpleCurl:(NSString*)urlStr withBody:(NSString*)post{

//    NSLog(urlStr);
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
//    NSLog([url description]);
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"close" forHTTPHeaderField:@"Connection"];
[req setValue:@"utf-8;q=0.7,*;q=0.7" forHTTPHeaderField:@"Accept-Charset"];
[req setValue:@"gzip,deflate" forHTTPHeaderField:@"Accept-Encoding"];
[req setValue:@"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" forHTTPHeaderField:@"User-Agent"];
[req setValue:@"Application/Json, text/html, application/xhtml+xml, */*" forHTTPHeaderField:@"Accept"];
[req setHTTPMethod:@"POST"];//  or PUT
NSString *postString = post;
[req setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

NSData *res = [NSURLConnection  sendSynchronousRequest:req returningResponse:NULL error:NULL];
NSString* html=[[NSString alloc] initWithData:res encoding:NSUTF8StringEncoding];
return html;

}

【讨论】:

  • 我不明白,这应该如何解决问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2014-09-23
  • 2021-02-16
  • 2017-08-17
  • 2015-12-02
  • 2016-09-25
  • 1970-01-01
相关资源
最近更新 更多