NSHTTPURLResponse statuscode returns 0 . 3 Out of 10 machines returns 0, in the rest

Discussion in 'Mac Programming' started by GokulKrish, Mar 30, 2015.

  1. GokulKrish, Mar 30, 2015
    Last edited by a moderator: Mar 30, 2015

    GokulKrish macrumors newbie

    Apr 12, 2014
    I am using this snippet to get data from an url .

    -(int) GetData : (NSString *) urlstr : (NSData **) bufferdata
         int retcode =0;
         NSError *errorobj = nil;
         NSHTTPURLResponse *httpresponse;
         NSData *buffer;
                  retcode = 0;
                  NSLog(@"url string:%@\n",urlstr);
                  NSURL *urlobj = [NSURL URLWithString: urlstr];
                  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlobj cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
                  [request setValue:@"close" forHTTPHeaderField:@"Connection"];
                  [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[urlobj host]];
                  buffer = [NSURLConnection sendSynchronousRequest:request returningResponse:&httpresponse error:&errorobj];
                  self->httpStatusCode =[B] [httpresponse statusCode];[/B]
                  [B]NSLog(@"Http response status code: %i: Description:%@\n",self->httpStatusCode,[NSHTTPURLResponse localizedStringForStatusCode:self->httpStatusCode]);[/B]
                  if(self->httpStatusCode == 200)
                      *bufferdata = buffer;
                      isconnectionSuccess = TRUE;
                  else {
                      *bufferdata = nil;
                      isconnectionSuccess = FALSE;
                      retcode = 1;
                  //errorobj will be set to some only if intended response is received
                  if (errorobj)
                      retcode =  [errorobj code];
                      NSLog(@"Error Description: %@\n", [errorobj localizedDescription]);
                      isconnectionSuccess = FALSE;
         @catch (NSException *exp) {
         NSLog(@"GetData: Exception occurred: %@ :reason: %@\n", [exp name],[exp reason]);
          retcode = ERROR_FAILURE;
         return retcode;

    Approximately 3 Out of 10 machines fails, but in rest 7 machines download goes fine .

    I am getting following error .

    Http response status code: 0: Description:server error
    Error Description: Could not connect to the server.

    Any ideas ? Thanks in advance ...
  2. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    We don't know what the other 7 machines are returning because you left that in a truncated subject line. Edit your post and describe the results there.
  3. GokulKrish thread starter macrumors newbie

    Apr 12, 2014
    In other 7 machines download was success, that means return code 200 . Thanks
  4. chown33 macrumors 604

    Aug 9, 2009
    The error description is "Could not connect to the server.", so that would be my first guess about what's wrong.

    Your code is using HTTP, so maybe you can use a simple web browser to test the URL from the failing machine.

    If the URL is actually HTTPS, maybe the problem is in the certificates. That's just a guess, since you didn't show any URLs.

    If the failing servers are ones you control, you should be able to look in their logs to see if the failing clients are actually establishing a connection or not.

    You might also be able to test the URLs using the 'curl' command. See its man page here:
    The simplest example of using curl is:
    curl "http://www.example.com/"
    You can copy and paste this into a Terminal window, and see what it does. There's a real "example.com" domain and server, maintained by ICANN:

    You didn't post the failing URL, nor say whether it's a dotted-quad or DNS name, nor say which network it's on (local subnet, enterprise network, at-large internet), so all anyone can do is make guesses about why the URL doesn't work.

    You'll have to provide more details about the specific URL, whether it's reachable from the failing machines by other means (e.g. curl or a browser), and so on.

    You may also need to look for common factors about the failing machines, such as they're all running a particular OS version, they're all on a particular subnet, or they all have a particular software install or setting (e.g. blocking software).

Share This Page