iphone开发 http post get

本文介绍了使用 Objective-C 通过 GET 和 POST 方法请求 iTunes API 的具体实现方式。其中包括构造 URL、设置请求头、发送请求等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.     } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值