我有一个网络服务。我使用它来接受小(缩略图大小)图像的base64字符串表示。当这个web服务与Fiddler一起使用并手动发布请求时,它的工作效果非常棒。当我使用NSMutableURLRequest (或ASIHTTPRequest)运行相同的请求时,它总是返回413状态代码(413表示请求实体太大)。
为什么NSMutableURLRequest会让它得到413,而Fiddler每次都会返回200?
这是我的NSMutableURLRequest代码。如果有人有什么想法的话,我真的需要一个推动力。
//the image request
NSMutableURLRequest *imageRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:POST_IMAGE_API_URL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:240.0];
//the post parameters
[imageRequest setHTTPMethod:@"POST"];
[imageRequest setHTTPBody:[imageMessage dataUsingEncoding:NSUTF8StringEncoding]];
[imageRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
//a few other things
NSURLResponse* imageresponse;
NSError *imageerror;
NSData* imageresult = [NSURLConnection sendSynchronousRequest:imageRequest returningResponse:&imageresponse error:&imageerror];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)imageresponse;
NSLog(@"imageresponse: %d", httpResponse.statusCode);发布于 2012-06-22 21:57:41
我找到了解决这个问题的办法。问题不在苹果一端,而在IIS一端。除了在WCF的web.config文件中指定每个服务的"uploadReadAheadSize“之外,对于IIS托管的应用程序(其中一个是我的WCF服务),还有一个额外的参数。我增加了这个值,413就消失了。足够有趣的是,当我从Fiddler发送HTTP请求时,在与服务所在的服务器相同的网络上的桌面客户端上,我没有得到这个错误。基本上,我有this guy's problem的解决方案,但没有他的上下文。我的解决方案是他的背景。
发布于 2012-06-21 00:28:32
当我看到你的这段代码时:
//the image request
NSMutableURLRequest *imageRequest =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:POST_IMAGE_API_URL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:240.0];我猜您的"POST_IMAGE_API_URL“#定义中有一些奇怪的字符,很可能是在您传递的参数中。
你需要URL encode the URL string你传递给你的URL请求。
尝试执行以下操作:
// assuming POST_IMAGE_API_URL starts with a "@" character
NSString * yourURLAsString = [NSString stringWithString: POST_IMAGE_API_URL];
NSURL * yourEncodedURL = [yourURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];并将"yourEncodedURL“作为参数传递给URLRequest。
https://stackoverflow.com/questions/11123661
复制相似问题