IOS development: Get and Post methods in iPhone

Source: Internet
Author: User

IOS developmentUnderstanding iPhoneGetAndPostThis article uses a simple example to show how the iPhone usesGetAndPostRequest asp.net webservice.

Webservice

1. Create a webservice,

2. enable http get and http post in webconfig.

 
 
  1. <webServices> 
  2.     <protocols> 
  3.         <add name="HttpSoap"/> 
  4.         <add name="HttpPost"/> 
  5.         <add name="HttpGet"/> 
  6.         <add name="Documentation"/> 
  7.     </protocols> 
  8. </webServices> 

Iphone client call:

1. get method:

 
 
  1. NSString *queryString =  
  2.           [NSString stringWithFormat:  
  3.     @“http://10.5.23.117:5111/Service1.asmx/HelloWorld?param=123%@“,  
  4.     ipAddress.text];  
  5.     NSURL *url = [NSURL URLWithString:queryString];  
  6.     NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];  
  7.     [req addValue:@“text/xml; charset=utf-8” forHTTPHeaderField:@“Content-Type”];  
  8.     [req addValue:0 forHTTPHeaderField:@“Content-Length”];  
  9.     [req setHTTPMethod:@“GET”];  
  10.     [activityIndicator startAnimating];  
  11.     conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];  
  12.     if (conn) {  
  13.         webData = [[NSMutableData data] retain];  
  14.     } 

2. post method:

 
 
  1. NSString *postString =@"123";    
  2.     NSURL *url = [NSURL URLWithString:    
  3. @“http://10.5.23.117:5111/Service1.asmx/HelloWorld2”];    
  4.     NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];    
  5.     NSString *msgLength = [NSString stringWithFormat:@“%d”, [postString length]];    
  6.     [req addValue:@“application/x-www-form-urlencoded”    
  7.         forHTTPHeaderField:@“Content-Type”];    
  8.     [req addValue:msgLength forHTTPHeaderField:@“Content-Length”];   
  9.     [req setHTTPMethod:@“POST”];   
  10.     [req setHTTPBody: [postString dataUsingEncoding:NSUTF8StringEncoding]];   
  11.     [activityIndicator startAnimating];  
  12.     conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];  
  13.     if (conn) {    
  14.         webData = [[NSMutableData data] retain];    
  15.     }  

Summary:IOS developmentUnderstanding iPhoneGetAndPostThis article is helpful to you!

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.