现在的位置: 首页 > 综合 > 正文

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

2017年11月14日 ⁄ 综合 ⁄ 共 3638字 ⁄ 字号 评论关闭

首先说说一下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 使用NSMutableURLRequest 
view plaincopy to clipboardprint? 
NSURL* url = [NSURLURLWithString:@"http://aminby.net"];     
NSMutableURLRequest* request = [NSMutableURLRequestnew];     
[requestsetURL:url];     
[requestsetHTTPMethod:@"GET"];     
NSHTTPURLRequest*response;     
NSData* data = [NSURLConnectionsendSynchronousRequest:request     
              returningResponse:&responseerror:nil];     
[NSString* strRet = [[NSString alloc] initWithData:dataencoding:NSUTF8String];     
NSLog(strRet);     
[strRetrelease];    
NSURL* url = [NSURL URLWithString:@"http://aminby.net"]; 
NSMutableURLRequest* request = [NSMutableURLRequest new]; 
[request setURL:url];  
[request setHTTPMethod:@"GET"];  
NSHTTPURLRequest* response;  
NSData* data = [NSURLConnection sendSynchronousRequest:request 
              returningResponse:&responseerror:nil];  
[NSString* strRet = [[NSString alloc] initWithData:dataencoding:NSUTF8String];  
NSLog(strRet);  
[strRetrelease];   

1.2 使用NSString 

view plaincopy to clipboardprint? 
/ options有两个枚举,NSMappedRead这个不懂,NSUncachedRead是不缓存     
[NSData dataWithContentsOfURL:(NSURL*)url     
        options:(NSUInteger)readOptionsMask     
        error:(NSError**)errorPtr]     
//  或者     
[NSData dataWithContentsOfURL:(NSURL*)url];    
/ options有两个枚举,NSMappedRead这个不懂, NSUncachedRead是不缓存 
[NSData dataWithContentsOfURL:(NSURL *)url 
        options:(NSUInteger)readOptionsMask 
        error:(NSError**)errorPtr]  
//  或者 
[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? 
NSURL* url = [NSURLURLWithString:@"http://aminby.net"];     
NSMutableURLRequest*  request= [NSMutableURLRequestnew];     
[requestsetURL:url];     
[request  setHTTPMethod:@"GET"];     
[request addValue:@"application/json"forHTTPHeaderField:@"Content-Type"];     
[request setHTTPBody:@"someparam"];     
NSHTTPURLRequest*response;     
NSData* data=  [NSURLConnectionsendSynchronousRequest:request     
         returningResponse:&responseerror:nil];     
[NSString* strRet=  [[NSString alloc]initWithData:dataencoding:NSUTF8String];     
NSLog(strRet);     
[strRet  release];

抱歉!评论已关闭.