我有一个奇怪的问题,我正在处理一个必须从服务器请求的项目,当URL
使用GET
method请求不带参数的方法时,它可以正常工作并返回所需的数据,但是当使用相同的代码来调用URL
并向其发送参数,它将失败,并显示以下错误:
error: Error Domain=NSURLErrorDomain Code=-1001 UserInfo=0xed4870 "timed out"
我的代码如下:
NSString *param = @"pageNumber=2";
NSURL *url = [NSURL URLWithString:@"http://myWebsite/api/Soccor/"];//When using this, it works fine
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myWebsite/api/Soccor?%@", param]];//When using this, it gives me the request time out error.
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"GET"];
[theRequest setTimeoutInterval:10];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
有什么线索为什么在发送带有参数的参数时会返回错误URL
?
我建议您考虑该请求的两个方面:
你是不是向我们展示您的设置msgLength
,但我只会建议您设定Content-Length
,如果您还使用头部setHTTPBody
,在这种情况下,你设置的Content-Length
是length
的NSData
,你作为一个参数使用setHTTPBody
。但这用于POST
请求,而不是GET
请求。
您application/json
为Content-Type
请求指定,但请求不包含JSON。如果您这样做是因为响应是JSON,则应注意此标头值是针对您的请求的,而不是针对服务器的响应。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句