12

每当我在可变请求上设置主体并且方法设置为 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];
4

3 回答 3

14

我尝试查找有关它的更多最新信息,但有一篇关于您所谈论的确切问题的旧帖子: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 请求将正常工作。

于 2010-08-12T15:36:42.560 回答
0

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

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

于 2010-08-12T15:23:56.227 回答
-1

这里是

+(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;

}

于 2013-05-17T21:23:50.163 回答