免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8970 | 回复: 1
打印 上一主题 下一主题

[iOS]网络编程专题:发送Http请求(POST GET)的方法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-04 16:43 |只看该作者 |倒序浏览
[iOS]网络编程专题:发送Http请求(POST GET)的方法








首先说说一下http请求
http请求最长用的方法是 get 和 post 方法

get方法和post方法相比理解起来比较简单,get方法可以直接请求一个url,也可以url后面拼接上参数作为一个新的url地址进行请求。form的enctype属性默认为application/x-www-form-urlencoded。不能发送二进制文件。
post方法相对要复杂一些。首先post方法要设置key和value ,所有的key和value都会拼接成 key1=value1&key2=value2的样式的字符串,然后这个字符串转化为二进制放到 http请求的body中。当请求发送的时候,也就跟随body一起传给服务器。http请求求Content-Type设置为:application/x-www-form-urlencoded。这里讲的只是简单的post请求,一般发送文件不会选择这种方式(从技术方面考虑也可以发送文件,就是把文件以 ke 和 value的方式放入)。下面我们再讨论一下post发送二进制文件更加普遍的方法。
post发送文件首先网页中的form的enctype设置为multipart/form-data,然后浏览器会把表单中需要提交的项目分隔,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。
如果有以下form,并选择了file1.txt上传
//作者:禚来强 电话:XXXXXX email:zhuolaiqiang@gmail.com
//原问地址: http://blog.csdn.net/diyagoanyhacker/article/details/6685398
<form action="http://server.com/cgi/handle"
enctype="multipart/form-data"
method="post">
<input type="text" name="submit-name" value="chmod777"><br />
What files are you sending? <input type="file" name="files"><br />
</form>

则有如下body:
Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="submit-name"

chmod777
--AaB03x
Content-Disposition: form-data; name="files"; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--AaB03x--
以上重点讲解的post请求的两种方式。如果还是不明白可以和我一起讨论。


发送Http请求(POST GET)的方法
我们知道Http有Get和Post两种方法,我们分开说吧.

另注: 今天讲的方法是同步的请求, 异步的方法我还没试过, 不知道有没有使用异步的需求, 有的话于发上来和大家分享.

1.Get方法
  1. 1.1 使用NSMutableURLRequest
  2. view plaincopy to clipboardprint?
  3. NSURL* url = [NSURLURLWithString:@"http://aminby.net"];     
  4. NSMutableURLRequest* request = [NSMutableURLRequestnew];     
  5. [requestsetURL:url];     
  6. [requestsetHTTPMethod:@"GET"];     
  7. NSHTTPURLRequest*response;     
  8. NSData* data = [NSURLConnectionsendSynchronousRequest:request     
  9.               returningResponse:&responseerror:nil];     
  10. [NSString* strRet = [[NSString alloc] initWithData:dataencoding:NSUTF8String];     
  11. NSLog(strRet);     
  12. [strRetrelease];   
  13. NSURL* url = [NSURL URLWithString:@"http://aminby.net"];
  14. NSMutableURLRequest* request = [NSMutableURLRequest new];
  15. [request setURL:url];  
  16. [request setHTTPMethod:@"GET"];  
  17. NSHTTPURLRequest* response;  
  18. NSData* data = [NSURLConnection sendSynchronousRequest:request
  19.               returningResponse:&responseerror:nil];  
  20. [NSString* strRet = [[NSString alloc] initWithData:dataencoding:NSUTF8String];  
  21. NSLog(strRet);  
  22. [strRetrelease];   
复制代码
1.2 使用NSString

view plaincopy to clipboardprint?
/
  1. options有两个枚举,NSMappedRead这个不懂,NSUncachedRead是不缓存     
  2. [NSData dataWithContentsOfURL:(NSURL*)url     
  3.         options:(NSUInteger)readOptionsMask     
  4.         error:(NSError**)errorPtr]     
  5. //  或者     
  6. [NSData dataWithContentsOfURL:(NSURL*)url];   
  7. / options有两个枚举,NSMappedRead这个不懂, NSUncachedRead是不缓存
  8. [NSData dataWithContentsOfURL:(NSURL *)url
  9.         options:(NSUInteger)readOptionsMask
  10.         error:(NSError**)errorPtr]  
  11. //  或者
  12. [NSData dataWithContentsOfURL:(NSURL*)url];   
复制代码
1.2和1.3的方法是缺点是没办法知道response的status,一般是返回200-299之间的数值代表请求成功.我们可以依照这个code来做数据处理,如果对地址存在很有把握,就可以使用后两种简单的GET方法.

今天查了一下手册,发现NSArray NSDictionary 也有xxxxWithContentsOfURL的方法,这两个我还没用过, 应该是跟NSData和NSString一样,但具体怎么用我还不清楚.

2.Post方法
2.1 使用NSMutableURLRequest
view plaincopy to clipboardprint?
  1. NSURL* url = [NSURLURLWithString:@"http://aminby.net"];     
  2. NSMutableURLRequest*  request= [NSMutableURLRequestnew];     
  3. [requestsetURL:url];     
  4. [request  setHTTPMethod:@"GET"];     
  5. [request addValue:@"application/json"forHTTPHeaderField:@"Content-Type"];     
  6. [request setHTTPBody:@"someparam"];     
  7. NSHTTPURLRequest*response;     
  8. NSData* data=  [NSURLConnectionsendSynchronousRequest:request     
  9.          returningResponse:&responseerror:nil];     
  10. [NSString* strRet=  [[NSString alloc]initWithData:dataencoding:NSUTF8String];     
  11. NSLog(strRet);     
  12. [strRet  release];
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-01-04 16:43 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP