











Jared Wiltshire 25th June 2010

I will explain what is required to send out a basic HTTP request and retrieve a response back from the server.

You should be able to see from this diagram where all the HTTP elements are used by which classes. The elements around the outside are the descriptive names for the HTTP elements and show what classes they are stored in.
Calling the HTTP request is done using the NSURLConnection class, allowing you to perform a synchronous request. Using this class allows you to specify the object that the response code string will be stored in and the object to store any error string returned by the HTTP request.
The response needs to be stored in an instance of NSData ready for you to interpret, which you can then convert to a string and deal with the response.
Let's look at some code that makes up a very basic HTTP request. First we will create our URL string, this should be the first thing you know anyway;
NSURL *oRequestUrl = [NSURL URLWithString:@"http://www.eigo.co.uk/Test-Submission.aspx"];
We need to add our HTTP headers but we have no request object. Create it by instantiating a new instance of the NSMutableURLRequest class;
NSMutableURLRequest *oRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[oRequest setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
[oRequest setHTTPMethod:@"POST"];
[oRequest setURL:oRequestUrl];
We need to create a HTTP Body, for our purposes let's enter a single line of text. Start by instantiating the NSMutable object and then append a single line of type NSString.
NSMutable oHttpBody = [NSMutableData data];
[oHttpBody appendData:[@"This is Http Request body" dataUsingEncoding:NSUTF8StringEncoding]];
Now that we have our HTTP Body; we can set the content length of the HTTP request as follows;
[oRequest setValue:[oHttpBody length] forHTTPHeaderField:@"Content-Length"];
Here we are accessing the NSMutable objects length property to obtain the size of the HTTP Body in bytes. Notice that we are using the same method that we did for the Content-Type to set the Content-Length.
Finally we need to create a response object that will call the request and store a response code and response data (if any). This is done with the NSURLConnection object in conjunction with an NSDataobject. Make sure you first create an object to store the HTTP Response Code and HTTP Error (just incase one is returned).
NSError *oError = [[NSError alloc] init];
NSHTTPURLResponse *oResponseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:oRequest returningResponse:oResponseCode error:oError];

Once this NSURLConnection object is initialised with the above objects, the HTTP request begins. TheNSData object we created above will contain the response from the server (if any), but the response code is what we want which is contained in the NSHTTPURLResponse object. The NSURLConnection object will pass the HTTP status code into this object.
We can check the status code by checking the status code value as follows;
If ([oResponseCode statusCode] >= 200)
{
NSLog(@"Status code is greater than 200");
}
NSString * strResult = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。