我有一个奇怪的问题,我正在处理一个必须从服务器请求的项目,当使用GET方法请求没有参数的GET时,它运行良好并返回所需的数据,但是当使用相同的代码调用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发送参数时返回错误的原因。
发布于 2013-09-25 08:14:24
我建议你考虑请求的两个方面:
msgLength设置,但我建议只在您也使用setHTTPBody的情况下设置一个Content-Length头,在这种情况下,您将Content-Length设置为用作setHTTPBody参数的NSData的length。但这是与POST请求一起使用的,而不是GET请求。application/json指定了Content-Type,但是请求不包含JSON。如果您这样做是因为响应是JSON,那么应该注意,这个标头值是针对您的请求的,而不是针对您期望的服务器响应的。https://stackoverflow.com/questions/18998333
复制相似问题