1

I've a strange problem, I'm working on a project that I've to request from a server, When requesting a URL without parameters using GET method, it works fine and return the desired data, but when using the same code to call the URL and sending a parameters to it, it fail with this error:

error: Error Domain=NSURLErrorDomain Code=-1001 UserInfo=0xed4870 "timed out"

My code is the following:

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];

Any clue why it returns error when sending parameters with the URL?

Scar
  • 3,460
  • 3
  • 26
  • 51
  • Once check your url format While passing parameters you may need to give any key for that param value. – kalyani puvvada Sep 25 '13 at 07:32
  • @kalyanipuvvada Could you please explain more !? Your comment is not clear to me. – Scar Sep 25 '13 at 07:37
  • @Rob I've tested the URL in the browser and it's working fine, that's the weirdest part. Regarding your point on the headers, why it works with the first URL, but the second doesn't !? – Scar Sep 25 '13 at 07:47
  • @Rob It seems you are right, the problem is `Content-Length`, When I've removed it, it works fine. Please answer the question so I accept it. But how come That error appear, if you have any explanation please tell me. – Scar Sep 25 '13 at 08:06
  • 1
    @Scar In terms of why precisely it behaves precisely the way it does when you send a malformed request, I don't know. I'd suspect that it depends upon what you set `msgLength` to (you never showed us that) and how your server is configured (I wouldn't be surprised to see different servers respond differently to these sorts of error conditions). – Rob Sep 25 '13 at 08:17
  • @Rob I've another issue, When requesting a URL with the following `http://myWebSite/api/soccor/10165`it suppose to return the soccor details, I've tested it on PostMan client (Chrome) it works fine, between, in the objective-c it does not work, do you have any idea why these things happen? – Scar Sep 25 '13 at 20:00
  • @Rob I've check the response, it's empty, when calling any service, it works fine and the response method `didReceiveData` is called probably, but in this service, this method is not called at all, it goes directly to `connectionDidFinishLoading` with empty respond. I will keep testing it and if I still have this error, I will ask another question, thank you for your assist :) – Scar Sep 25 '13 at 21:27

1 Answers1

1

I would suggest you consider two aspects of the request:

  1. You are not showing us your setting of msgLength, but I would only suggest setting a Content-Length header if you're also using setHTTPBody, in which case you'd set the Content-Length to be the length of the NSData you use as a parameter to setHTTPBody. But this is used with POST requests, not GET requests.

  2. You specify application/json for the Content-Type of the request, but request does not consist of JSON. If you did that because the response is JSON, it should be noted that this header value is for your request, not for the response you expect from the server.

Rob
  • 415,655
  • 72
  • 787
  • 1,044