IOS ProgramOfHTTPSynchronizationRequestAnd asyncRequestThe solution to the problem is described in this article. There is not much content, mainly through code to achieve synchronous and asynchronousRequest, Let's take a look at the details.
Asynchronous request,
- NSMutableData * buf = [[NSMutableData alloc] initWithLength: 0];
- NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: req delegate: self];
- // Triggered when the response is received
- -(Void) connection :( NSURLConnection *) aConnection didReceiveResponse :( NSURLResponse *) aResponse;
- // You can determine the returned result or process the information in the returned http header.
- // Call each time data is received
- -(Void) connection :( NSURLConnection *) aConn didReceiveData :( NSData *) data;
- // Therefore, in general, yes
- -(Void) connection :( NSURLConnection *) aConn didReceiveData :( NSData *) data
- {
- [Buf appendData: data];
- }
- // Of course, buffer is also stated in the previous initWithRequest.
- // Triggered when a network error occurs
- -(Void) connection :( NSURLConnection *) aConn didFailWithError :( NSError *) error;
- // Triggered when all data is received
- -(Void) connectionDidFinishLoading :( NSURLConnection *) aConn;
Synchronous request
- // For initialization, please request NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
- // Set the URL
- [Request setURL: [NSURL URLWithString: urlStr];
- // Set the HTTP Method
- [Request setHTTPMethod: @ "GET"];
- // Sending and receiving synchronization request. Here, the returnData is the returned data records.
- NSData * returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
- // Configure the object
- [Request release];
Summary:IOS ProgramOfHTTPSynchronizationRequestAnd asyncRequestI hope this article will help you solve the problem!