Send HTTP POST Request Through XML In iOS
Suppose you want to send the HTTP POST request to the server with some parameters and XML Data.So firstly you want to create your XML , but the problem is that iOS not provide the simple way to create the XML . Then you have to create the XML String that you want to send to Server Side. XML data :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
xmlString =[NSString stringWithFormat:@"<prestashop xmlns=\"http://www.w3.org/1999/xlink\">" "<address>" "<id_customer>%@</id_customer>" "<active>1</active>" "<id>%@</id>" "<company>Webkul</company>" "<id_country>%@</id_country>" "<firstname>%@</firstname>" "<lastname>%@</lastname>" "<address1>%@</address1>" "<address2>%@</address2>" "<city>%@</city>" "<postcode>%@</postcode>" "<phone_mobile>%@</phone_mobile>" "<phone>%@</phone>" "<alias>%@</alias>" "<other>%@</other>" "</address>" "</prestashop>"@"20",@"56",@"ram",...........]; |
After creating the XML String, you have to send the request to the server with XML string and some parameters.If the request is successfully sent to the server,you will get some data. And use that data according to your need. here is the code using POST Request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
SString *BaseDomain = URL; NSMutableString *params = [NSMutableString string]; [params appendFormat:@"id_currency=%@&", [preferences objectForKey:@"id_currency"]]; [params appendFormat:@"ws_key=%@&", WS_KEY]; [params appendFormat:@"id_lang=%@", [preferences objectForKey:@"id_lang"]]; BaseDomain = [BaseDomain substringToIndex:[BaseDomain length] - 8]; NSString *urlString = [NSString stringWithFormat:@"%@%@?%@",BaseDomain, @"customers",params]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%lu", [xmlString length]]; [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; webData = [[NSMutableData alloc] init]; [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; [theRequest setHTTPMethod:@"POST"]; [theRequest setHTTPBody: [xmlString dataUsingEncoding:NSUTF8StringEncoding]]; [NSURLConnection sendAsynchronousRequest:theRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){ |