Wednesday, May 1, 2013

NSURLRequest HTTPBody example ios


HTTPBody

Returns the receiver’s HTTP body data.
- (NSData *)HTTPBody
Return Value of [NSURLRequest HTTPBody]
The receiver's HTTP body data.
Discussion of [NSURLRequest HTTPBody]
This data is sent as the message body of a request, as in an HTTP POST request.
Example of [NSURLRequest HTTPBody]
NSData *data = [request HTTPBody];
[request setHTTPBody:[[jsonPostBody stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
                      dataUsingEncoding:NSUTF8StringEncoding 
                   allowLossyConversion:YES]];    
Example of [NSURLRequest HTTPBody]
NSURL *aUrl = [NSURL URLWithString:@"http://www.apple.com/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];

NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request 
                                                             delegate:self];

[request setHTTPMethod:@"POST"];
NSString *postString = @"company=Locassa&quality=AWESOME!";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];