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

    Joined:
    Apr 12, 2014
    Location:
    Chennai
    #1
    I am using this snippet to get data from an url .


    Code:
    -(int) GetData : (NSString *) urlstr : (NSData **) bufferdata
    {
         int retcode =0;
         NSError *errorobj = nil;
         NSHTTPURLResponse *httpresponse;
         NSData *buffer;
     
         @try
          {
                  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 .


    Code:
    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

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    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

    Joined:
    Apr 12, 2014
    Location:
    Chennai
    #3
    In other 7 machines download was success, that means return code 200 . Thanks
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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:
    https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/curl.1.html
    The simplest example of using curl is:
    Code:
    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:
    http://en.wikipedia.org/wiki/Example.com


    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