iOS网络编程 GET和POST

本文详细介绍了web服务接口调用中的HTTP请求方式,包括GET和POST的区别及实现方法,以及如何使用iOS类库和ASIHTTPRequest进行POST操作。此外,还提供了构造完整请求头和消息报文的方法,适用于复杂的数据传输场景。

最近项目开发用到各种各样的web服务接口调用,顺便记录下,和一些链接,防止再找找不到。

=====================

客户端向web服务器发送HTTP请求常用方式是GET和POST。两者的不同在于传参方式的不同。GET方式就是通过http://yourhost:port/URI之后附加拼接参数后的URL访问服务器;POST是手动填充HTTP Body内容再访问http://yourhost:port/URI.

iOS自带类库 实现POST

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kDomain]];
[request setHTTPMethod:@"POST"]; 
NSDictionary *content = [NSDictionary dictionaryWithObjectsAndKeys: self.head, @"head", self.body, @"body", nil];
NSString *messageStr = [content JSONString]; 
messageStr = [NSString stringWithFormat:@"reqData=%@", messageStr]; 
NSData *messageData = [messageStr dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
[request setHTTPBody:messageData]; 
NSHTTPURLResponse *response; 
NSError *error; 
NSData *data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error]; 
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

ASIHTTPRequest实现POST功能更容易:实例化ASIFormDataRequest对象,执行addPostValue:forKey:方法添加你想要传递的参数,用addFile、addData上传文件。

参考  ASIHttpRequest-发送数据

=======================

之后在网上找到另一个解决方法是构造完整的请求头和消息报文,填充到HTTP Body。

POST http://yourhost:port/URI  HTTP/1.1 
  Accept: text/plain, */* 
  Accept-Language: zh-cn 
  Host: yourhost
  Content-Type:multipart/form-data;boundary=-----------------------------7db372eb000e2//即为boundary的标志,用来表示一段内容的开始
  User-Agent: WinHttpClient 
  Content-Length: 3693
  Connection: Keep-Alive
(以上为协议的头)
  -------------------------------7db372eb000e2
  Content-Disposition: form-data; name="file"; filename="img.jpg"
  Content-Type: image/jpeg
  (此处省略jpeg文件二进制数据...)
  -------------------------------7db372eb000e2--

如果上传的是表单Form,则用分隔符bounday之前添加"--"来分开每一个键值对。bounday之后添加"--"表示结束。 iOS实现:

NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL: url]; 
[req setHTTPMethod:@"POST"]; 
[req setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDRY] forHTTPHeaderField:@"Content-Type"]; 
NSMutableData *postData =[NSMutableData dataWithCapacity:512]; 
[postData appendData: [[NSString stringWithFormat:@"--%@/r/n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]]; int i=0; 
int cnt = data.count; 
for ( NSString *key in [data allKeys]) {
	[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=/"%@/"/r/n/r/n", key ] dataUsingEncoding:NSUTF8StringEncoding]]; 
	[postData appendData:[[NSString stringWithFormat:@"%@",[data objectForKey:key ]] dataUsingEncoding:NSUTF8StringEncoding]]; 
	if( i != cnt -1 ) {
		 [postData appendData:[[NSString stringWithFormat:@"/r/n--%@/r/n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]];
	}
	i++ ;
 }
[postData appendData:[[NSString stringWithFormat:@"/r/n--%@--/r/n",BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]]; 
[req setHTTPBody:postData];

参考:

http以post方式上传一个文件,构造其请求头和消息报文

 iPHone 中用 NSURLRequest 模拟 POST 和GET 请

iPhone网络编程–一起来做网站客户端 主贴

Http协议详解

深入理解http协议

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值