iphone开发 http post get

来源:互联网 发布:javascript实例精通 编辑:程序博客网 时间:2024/06/13 11:19

1、get的方式:

  1. NSString *queryString =  
  2.           [NSString stringWithFormat:  
  3.     @“http://itunes.apple.com/search?term=TacTraHD&entity=software];  
  4.     NSURL *url = [NSURL URLWithString:queryString];  
  5.     NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];  
  6.     [req addValue:@“text/xml; charset=utf-8” forHTTPHeaderField:@“Content-Type”];  
  7.     [req addValue:0 forHTTPHeaderField:@“Content-Length”];  
  8.     [req setHTTPMethod:@“GET”];  
  9.     [activityIndicator startAnimating];  
  10.     conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];  
  11.     if (conn) {  
  12.         webData = [[NSMutableData data] retain];  
  13.     } 

2、post的方式:

  1. NSString *postString =@"123";    
  2.     NSURL *url = [NSURL URLWithString:    
  3. @“http://itunes.apple.com/search?term=TacTraHD&entity=software”];    
  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.     }