how do I check an http request response status code from iOS?

后端 未结 2 1440
無奈伤痛
無奈伤痛 2020-12-16 21:03

I am sending an http request from iOS (iPad/iPhone) to my python google app engine server using the NSURLConnection and NSURLRequest classes.

相关标签:
2条回答
  • 2020-12-16 21:14

    If you are sending a synchronous request, you could get the response code from NSHTTPURLResponse.

    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL_LOGIN]];
    NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"~~~~~ Status code: %d", [response statusCode]);
    

    Hope this will help you :)

    0 讨论(0)
  • 2020-12-16 21:32

    The connection:didReceiveResponse: delegate method is called when a response is received, which gives you an NSURLResponse to play with.

    If you've made an HTTP request, then it'll actually be an NSHTTPURLResponse object, which has a statusCode method.

    0 讨论(0)
提交回复
热议问题