Thursday, January 2, 2014

iphone http communication

Hello everyone, I am new to iphone soon, now have a http communication task.
Would like to ask you a master, to achieve http what kind?
Can you put the sample source code?
Thank you. .
Reply:
 / / prepar request 
NSString * urlString = [NSString stringWithFormat: @ "http://urlToSend.com"];
NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];
[Request setURL: [NSURL URLWithString: urlString]];
[Request setHTTPMethod: @ "POST"];

/ / Set headers
NSString * contentType = [NSString stringWithFormat: @ "text / xml"];
[Request addValue: contentType forHTTPHeaderField: @ "Content-Type"];

/ / Create the body
NSMutableData * postBody = [NSMutableData data];
[PostBody appendData: [[NSString stringWithFormat: @ ""] dataUsingEncoding: NSUTF8StringEncoding]];
[PostBody appendData: [[NSString stringWithFormat: @ ""] dataUsingEncoding: NSUTF8StringEncoding]];
[PostBody appendData: [[NSString stringWithFormat: @ "
"] dataUsingEncoding: NSUTF8StringEncoding]];

/ / Post
[Request setHTTPBody: postBody];

/ / Get response
NSHTTPURLResponse * urlResponse = nil;
NSError * error = [[NSError alloc] init];
NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: & urlResponse error: & error];
NSString * result = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
NSLog (@ "Response Code:% d", [urlResponse statusCode]);
if ([urlResponse statusCode]> = 200 && [urlResponse statusCode] <300) {
NSLog (@ "Response:% @", result);

/ / Here you get the response

}

Reply:
Thank you. . . . .
Reply:
cited a floor cloudhsu reply:
C / C + + code
/ / Prepar request
NSString * urlString = [NSString stringWithFormat: @ "http://urlToSend.com"];
NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];
......

Is so grateful!

No comments:

Post a Comment