
正文
iOS上的http请求:get、post以及同步、异步
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1、get:
view
sourceprint
sourceprint
" class="item about" style="color:rgb(51,51,51); text-decoration:none; margin:0px 0px 0px 8px; padding:0px; border-width:0px 0px 1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51); outline:0px; float:left; vertical-align:baseline; position:static; left:auto; top:auto; right:auto; bottom:auto; height:16px; width:16px; display:block; overflow:hidden; text-indent:-5000px">?
01.<span
style="font-size:14px;">NSString
* URLString = @"theRegionCode=" style="color:rgb(51,51,51); text-decoration:none; border-bottom-width:1px; border-bottom-color:rgb(51,51,51); border-bottom-style:dotted">http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityDataset?theRegionCode=
广东";02.NSURL
* URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];03. 04.NSURLRequest
* request = [[NSURLRequest alloc]initWithURL:URL];05.NSURLResponse
* response = nil;06.NSError
* error = nil;07.NSData
* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];08.if (error)
{09.NSLog(@"error:
%@",[error
localizedDescription]);10.}else{11.NSLog(@"response
: %@",response);12.NSLog(@"backData
: %@",[[NSString
alloc]initWithData:data encoding:NSUTF8StringEncoding]);13.}14.</span>get方法的请求參数是放在长长的URL字符串里面。这里仅仅须要一个參数,就是地区的编号或名字,通过这个參数,server返回属于这个区域内支持天气查找的城市列表。假设有很多其它的參数也是放在URL字符串里面,至于參数的组织方式,就看server的要求了。
用字符串构建NSURL,最好在使用URLWithString的时候把原字符串进行一下UTF8转码。关于为何要转码。看下这里第一部分。然后NSURL对象构建NSURLRequest。使用NSURLConnection的同步方法。传入request对象就能够通过get方法获取数据。
这里有个NSError对象地址传入,用于做错误推断。网络的实际情况是多变的。必需要考虑请求错误的情况。否则可能导致程序奔溃。
2、post:
view
sourceprint?
sourceprint?
01.NSString
* URLString = @"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString";02.NSURL
* URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];03. 04.NSString
* postString = @"theRegionCode=广东";05.NSData
* postData = [postString dataUsingEncoding:NSUTF8StringEncoding]; //将请求參数字符串转成NSData类型06. 07.NSMutableURLRequest
* request = [[NSMutableURLRequest alloc]init];08.[request
setHTTPMethod:@"post"]; //指定请求方式09.[request
setURL:URL]; //设置请求的地址10.[request
setHTTPBody:postData]; //设置请求的參数11. 12.NSURLResponse
* response;13.NSError
* error;14.NSData
* backData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];15. 16.if (error)
{17.NSLog(@"error
: %@",[error
localizedDescription]);18.}else{19.NSLog(@"response
: %@",response);20.NSLog(@"backData
: %@",[[NSString
alloc]initWithData:backData encoding:NSUTF8StringEncoding]);21.}post方式的时候參数是放在HTTPBody里面的,并且须要将字符串转码成响应的NSData类型。在接口文档里一般都有指出转码方式。须要按指定方式转码,这里的UTF8,也有gb2312的。request构建好了之后,和get方法一样使用NSURLConnection请求数据。
3、同步和异步请求:
一般网络请求都须要一段时间,哪怕数据再少、网络再好。都会有一段时间,并且非常多时候必须考虑在网络不好的时候的app状态。
使用同步请求仅仅需安心等待数据就能够。不须要做额外操作,上面两例都是同步请求。connection调用方法后会把返回请求的数据,无需做什么其它事。可是同步会堵塞线程,假设通过点击button来发起请求,那么按钮会停留在highLight状态直到请求结束,会造成一种app卡住、死机的感觉,非常不好。
异步get:
view
sourceprint?
sourceprint?
1.NSString
* URLString = @"theRegionCode=" style="color:rgb(51,51,51); text-decoration:none; border-bottom-width:1px; border-bottom-color:rgb(51,51,51); border-bottom-style:dotted">http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityDataset?theRegionCode=
广东";2.NSURL
* URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];3. 4.NSURLRequest
* request = [[NSURLRequest alloc]initWithURL:URL];5. 6._connection
= [[NSURLConnection alloc]initWithRequest:request delegate:self]; (1)异步post类似,不再是使用NSURLConnection调用方法直接得到数据,而是使用(1)位置方法构建一个NSURLConnection对象,这种方法会默认開始请求数据。接下来关键就是靠托付了。由于请求的时间未知,所以使用托付模式的回调作用。在数据回来是调用协议方法。post和get托付方法处理一样。
协议方法:
要注意的是这里有两个托付:NSURLConnectionDataDelegate和NSURLConnectionDelegate,前一个继承于后一个。获取数据的方法是定义在前一个托付里面的。所以仅仅要遵循NSURLConnectionDataDelegate就能够了。
一般用到四个托付方法:
view
sourceprint?
sourceprint?
01.<pre
name="code" class="objc">//接受到respone,这里面包括了HTTP请求状态码和数据头信息。包括数据长度、编码格式等02.-(void)connection:(NSURLConnection
*)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"response
= %@",response);
_backData = [[NSMutableData alloc]init];03.}04. 05.//接受到数据时调用,完整的数据可能拆分为多个包发送。每次接受到数据片段都会调用这种方法,所以须要一个全局的NSData对象。用来把每次的数据拼接在一起06.-(void)connection:(NSURLConnection
*)connection didReceiveData:(NSData *)data{07.[_backData
appendData:data];08.}09. 10.//数据接受结束时调用这种方法,这时的数据就是获得的完整数据了,能够使用数据做之后的处理了11.-(void)connectionDidFinishLoading:(NSURLConnection
*)connection{12.NSLog(@"%@",[[NSString
alloc]initWithData:_backData encoding:NSUTF8StringEncoding]);13.}14. 15.//这是请求出错是调用,错误处理不可忽视16.-(void)connection:(NSURLConnection
*)connection didFailWithError:(NSError *)error{17.if (error.code
== NSURLErrorTimedOut) {18.NSLog(@"请求超时");19.}20.NSLog(@"%@",[error
localizedDescription]);21.}view
sourceprint?
sourceprint?
1. view
sourceprint?
sourceprint?
1. view
sourceprint?
sourceprint?
1. 最后,请求能够设置超时时间:
view
sourceprint
sourceprint
" class="item about" style="color:rgb(51,51,51); text-decoration:none; margin:0px 0px 0px 8px; padding:0px; border-width:0px 0px 1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51); outline:0px; float:left; vertical-align:baseline; position:static; left:auto; top:auto; right:auto; bottom:auto; height:16px; width:16px; display:block; overflow:hidden; text-indent:-5000px">?
1.NSURLRequest
* request = [[NSURLRequest alloc]initWithURL:URL cachePolicy:0 timeoutInterval:8.0];</span>或者:
view
sourceprint?
sourceprint?
1.NSMutableURLRequest
* request = [[NSMutableURLRequest alloc]initWithURL:URL];2.[request
setTimeoutInterval:8.0];请求时间超过所设置的超时时间。会自己主动调用
view
sourceprint
sourceprint
" class="item about" style="color:rgb(51,51,51); text-decoration:none; margin:0px 0px 0px 8px; padding:0px; border-width:0px 0px 1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51); outline:0px; float:left; vertical-align:baseline; position:static; left:auto; top:auto; right:auto; bottom:auto; height:16px; width:16px; display:block; overflow:hidden; text-indent:-5000px">?
1.-(void)connection:(NSURLConnection
*)connection didFailWithError:(NSError *)error可是有个问题是怎么把推断是超时导致的请求失败,上面的样例里已经写了,能够依据返回的error的code进行推断。了解不同情况的请求失败,能够更好的给用户提示。





