小编Mar*_*ark的帖子

当方法不是POST时,NSURLRequest无法处理HTTP正文?

每当我在一个可变请求上设置一个主体,并将该方法设置为除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];
Run Code Online (Sandbox Code Playgroud)

iphone cocoa nsurlrequest

12
推荐指数
2
解决办法
1万
查看次数

标签 统计

cocoa ×1

iphone ×1

nsurlrequest ×1