Using ASIHTTPRequest for Sync Http Request


// Sending Synchronous HTTP Request using AISHTTP library is very easy. // Since the request is synchronous, no callback method is required for the same. // You can specify the request timeout as desired. // The following snippet shows how to send and get the sync request and get the response. // Asychornous Http Request with ASIHTTP // The sendAndReceive method sends the request. – (GDataXMLDocument*) sendAndReceive: (NSString *) restUrl withError:(NSError**) error { debugLog(@"URL : %@",restUrl); ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL: [NSURL URLWithString:restUrl]]; [request setTimeOutSeconds:REQUEST_TIMEOUT]; [request setAllowCompressedResponse:NO]; [request setDefaultResponseEncoding:NSUTF8StringEncoding]; [request startSynchronous]; NSError *httpError = [request error]; if (!httpError) { NSString *responseXml = [[request responseString] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; // responseXml = @"<!–?xml version="1.0" encoding="UTF-8"?–>10"; // NSData *data = [responseXml dataUsingEncoding: NSUTF8StringEncoding]; debugLog(@"response %@", responseXml); GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:responseXml options: 0 error: error]; return doc; } else { error = &httpError; return NULL; } } // Best practices stipulate that HTTP request should not block so make the GUI responsive (enhance user experience) // so if you can consider running http request in Async mode.

Loading Please Wait...