IPhone implements xml post transmission and receives data

Source: Internet
Author: User

IPhone implements xml post transmission and receivesDataThis is the content to be introduced in this article. This article is based on code implementation. During iphone development, data exchange with the server is often encountered. The following code is to passXml dataStream, Server AcceptDataAnd returnsXml dataType.

Code for iphone data transmission:

 
 
  1. -(void) postxml   
  2. {   
  3.     //prepar request   
  4.     NSString *urlString = [NSString stringWithFormat:@"path"];   
  5.     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];   
  6.     [request setURL:[NSURL URLWithString:urlString]];   
  7.     [request setHTTPMethod:@"POST"];   
  8.     //set headers   
  9.     NSString *contentType = [NSString stringWithFormat:@"text/xml"];   
  10.     [request addValue:contentType forHTTPHeaderField: @"Content-Type"];   
  11.     //create the body   
  12.     NSMutableData *postBody = [NSMutableData data];   
  13.     [postBody appendData:[[NSString stringWithFormat:@"<Request  Action=\"Login\">"] dataUsingEncoding:NSUTF8StringEncoding]];   
  14.     [postBody appendData:[[NSString stringWithFormat:@"<Body>"] dataUsingEncoding:NSUTF8StringEncoding]];   
  15.     [postBody appendData:[[NSString stringWithFormat:@"<Username>wangjun</Username>"] dataUsingEncoding:NSUTF8StringEncoding]];   
  16.     [postBody appendData:[[NSString stringWithFormat:@"<Password>password</Password>"] dataUsingEncoding:NSUTF8StringEncoding]];   
  17.     [postBody appendData:[[NSString stringWithFormat:@"<PlatformID>2</PlatformID>"] dataUsingEncoding:NSUTF8StringEncoding]];   
  18.     [postBody appendData:[[NSString stringWithFormat:@"<PlatformVersion>3.1.3</PlatformVersion>"] dataUsingEncoding:NSUTF8StringEncoding]];   
  19.     [postBody appendData:[[NSString stringWithFormat:@"<TaskViewerName>IP 1.3</TaskViewerName>"] dataUsingEncoding:NSUTF8StringEncoding]];   
  20.     [postBody appendData:[[NSString stringWithFormat:@"<TaskViewerVersion>3</TaskViewerVersion>"] dataUsingEncoding:NSUTF8StringEncoding]];   
  21.     [postBody appendData:[[NSString stringWithFormat:@"</Body>"] dataUsingEncoding:NSUTF8StringEncoding]];   
  22.     [postBody appendData:[[NSString stringWithFormat:@"</Request>"] dataUsingEncoding:NSUTF8StringEncoding]];   
  23.     //post   
  24.     [request setHTTPBody:postBody];   
  25.     //get response   
  26.     NSHTTPURLResponse* urlResponse = nil;    
  27.     NSError *error = [[NSError alloc] init];    
  28.     NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];    
  29.     NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];   
  30.     NSLog(@"Response Code: %d", [urlResponse statusCode]);   
  31.     if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {   
  32.         NSLog(@"Response: %@", result);   
  33.     }   

The running result is:

 
 
  1. 13:19:25. 190 PostXmlDemo1 [685: 307] Click me, haha
  2. 13:19:28. 450 PostXmlDemo1 [685: 307] Response Code: 200
  3. 13:19:28. 456 PostXmlDemo1 [685: 307] Response:
  4. <Response Action = 'login'> <Cookie> DFGHJYUIILNBV $ % ^ & * gghj ^ & IUF % ^ R ^ DFYUIHNVB
  5. </Cookie> <Body> <User Name = 'username'> <Avatar> login User profile URL </Avatar> </User> <State> 1 </State> <Description>
  6. Description after successful login. </Description> </Body> </Response>

Summary

IPhoneImplementationXMLOfPOSTPass and receiveDataI hope this article will help you.

From:> http://wangjun.easymorse.com /? P = 864

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.