Making GET and POST Requests from an iPhone Application - Clarification Needed

后端 未结 3 1415
半阙折子戏
半阙折子戏 2020-12-29 16:13

I\'m following along with this useful looking answer to my question.

It seems to be working, but here are two questions I have:

  1. How do I detect an HTTP
3条回答
  •  不思量自难忘°
    2020-12-29 17:03

    You will get the status code returned in the didReceiveResponse

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        NSHTTPURLResponse *httpResponse;
        httpResponse = (NSHTTPURLResponse *)response;
        int statusCode = [httpResponse statusCode];
        //statusCode will be the http code returned like 201,500
     }
    

    For stopping the connection, use a class-level variable for the connection. The best way to go about it would be create a wrapper which sends requests and receives response. Make your viewcontroller a delegate of this class and whenever the didReceiveResponse gives an error status code, call the appropriate method of the delegate and stop the connection.

    Here's a good wrapper class example

    http://kosmaczewski.net/projects/objective-c-rest-client/

提交回复
热议问题