iphone – NSURLRequest在方法不POST时无法处理HTTP主体?

iphone – NSURLRequest在方法不POST时无法处理HTTP主体?,第1张

概述每当我将一个身体设置为可变请求,方法设置为除POST之外的任何东西,则身体不会包含在请求中,并且当服务器回复时,我会收到一个kCFErrorDomainCFNetwork错误303(kCFErrorHTTPParseFailure).将方法更改为POST是所有请求通过而没有错误.有没有办法附加身体到其他方法,还是我们坚持使用POST的一切? 这是提交代码: NSMutableURLRequest 每当我将一个身体设置为可变请求,方法设置为除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 JsONSBJsonWriter *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

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

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

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

博客文章:

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

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

总结

以上是内存溢出为你收集整理的iphone – NSURLRequest在方法不POST时无法处理HTTP主体?全部内容,希望文章能够帮你解决iphone – NSURLRequest在方法不POST时无法处理HTTP主体?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1029248.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存