当方法不是POST时,NSURLRequest无法处理HTTP正文? [英] NSURLRequest cannot handle HTTP body when method is not POST?

查看:148
本文介绍了当方法不是POST时,NSURLRequest无法处理HTTP正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在一个可变请求上设置一个正文,方法设置为POST以外的任何东西时,正文不包含在请求中,当服务器回复时,我得到一个kCFErrorDomainCFNetwork错误303(kCFErrorHTTPParseFailure)。将方法更改为POST是请求通过而无错误所需的全部内容。有没有办法将身体附加到其他方法,或者我们是否坚持使用POST?

Whenever I set a body on a mutable request with the method set to anything other than POST, the body is not included in the request and I get a kCFErrorDomainCFNetwork error 303 (kCFErrorHTTPParseFailure) when the server replies. Changing the method to POST is all it takes for the request to go through with no error. Is there any way to attach a body to other methods, or are we stuck with POST for everything?

这是提交代码:

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];


推荐答案

我尝试了解有关它的更多最新信息,但那里是一篇关于你所谈论的确切问题的老帖子: http: //lists.apple.com/archives/cocoa-dev/2004/May/msg01847.html

I tried finding more recent information about it, but there was an old post about the exact issue you were talking about: http://lists.apple.com/archives/cocoa-dev/2004/May/msg01847.html

基本上,他们提到这是一个错误码。可悲的是,我希望我能找到更新的确认它的东西。

Basically, they mention it is a bug in the code. Sadly, I wished I could find something more recent that confirms it.

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

The code I've been using is ASIHTTPRequest and it can definitely do PUT requests, since they use a lower level set of code to create the HTTP messages and don't rely on the NSMutableUrlRequest.

此外,我发现另一篇博客文章谈论了这个问题以及你需要为PUT做些什么请求。 http://iphonedevelopment.blogspot.com/2008/06/http -put-and-nsmutableurlrequest.html

Also, I found another blog post talking about the issue and what you need to put for a PUT request. http://iphonedevelopment.blogspot.com/2008/06/http-put-and-nsmutableurlrequest.html


当使用NSMutableURLRequest执行HTTP PUT请求时,添加以下代码行(req是NSMutableURLRequest):

When using NSMutableURLRequest to do an HTTP PUT request, add the following line of code (req is the NSMutableURLRequest):



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




这就是它的全部内容。如果你添加这行代码,你的PUT请求就可以正常工作。

That's all there is to it. If you add this line of code, your PUT requests will work just fine.

这篇关于当方法不是POST时,NSURLRequest无法处理HTTP正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆