网络请求的基本知识 我们网络请求用的是HTTP请求
Http请求格式:请求的方法,请求头,请求正文
Http请求的Request fields:请求的头部,以及被请求头部的一些设置
Http请求的Response fields:请求的回复头,通过回复头可以了解服务器给我们的一些状态信息。
项目源码:链接: http://pan.baidu.com/s/1i37yGzN 密码: kw6s
![](http://static.oschina.net/uploads/img/201603/11172617_D3Dg.png)
![](http://static.oschina.net/uploads/img/201603/11172617_F0P7.png)
![](http://static.oschina.net/uploads/img/201603/11172617_hxcE.png)
![](http://static.oschina.net/uploads/img/201603/11172617_8gwR.png)
![](http://static.oschina.net/uploads/img/201603/11172617_EGCF.png)
![](http://static.oschina.net/uploads/img/201603/11172617_BI02.gif)
![](http://static.oschina.net/uploads/img/201603/11172617_c5dq.gif)
1 - (void)viewDidLoad {
2 [super viewDidLoad];
3 // 两个网址:
4 // http://api.openweathermap.org/data/2.5/forecast/daily?lat=39.907501&lon=116.397232&cnt=10
5
6 // http://h.hiphotos.baidu.com/image/pic/item/8d5494eef01f3a298a1c0a799c25bc315d607cb5.jpg
7
8 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
9
10 // 设置回复内容信息
11 manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
12
13 AFHTTPRequestOperation *httpOperation = [manager GET:@""
14 parameters:parameters
15 success:^(AFHTTPRequestOperation *operation, id responseObject) {
16
17 }
18 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
19
20 }];
21
22 }
再修改了一点代码:
![](http://static.oschina.net/uploads/img/201603/11172618_Wza9.png)
![](http://static.oschina.net/uploads/img/201603/11172617_BI02.gif)
![](http://static.oschina.net/uploads/img/201603/11172617_c5dq.gif)
1 - (void)viewDidLoad {
2 [super viewDidLoad];
3 // 两个网址:
4 // http://api.openweathermap.org/data/2.5/forecast/daily?lat=39.907501&lon=116.397232&cnt=10
5
6 // http://h.hiphotos.baidu.com/image/pic/item/8d5494eef01f3a298a1c0a799c25bc315d607cb5.jpg
7
8 NSString* urlStr = @"http://api.openweathermap.org/data/2.5/forecast/daily?lat=39.907501&lon=116.397232&cnt=10";
9
10 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
11
12 // 设置回复内容信息
13 manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
14
15 [manager GET:urlStr
16 parameters:nil
17 success:^(AFHTTPRequestOperation *operation, id responseObject) {
18 NSLog(@"%@",responseObject);//查看回复的信息
19 }
20 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
21 NSLog(@"%@",error);//以及查看错误信息
22 }];
23 }
运行之后就能打印出网页数据。
经过一个简单的实例后,讲讲HTTP的请求格式:
// = HTTP请求格式 =
// ------------------------------
// * 请求方法 (GET、POST等) *
// * 请求头 (HttpHeaderFields) *
// * 请求正文 (数据) *
// ------------------------------
首先需要知道的是请求方法,有很多请求方法,其中最常用的是GET、POST方法,上面的实例使用的是GET请求,如果改为POST请求:
其它和GET请求一样:
然后运行就同样可以有数据结果。同时说明,那个网站即支持GET请求,也支持POST请求。
好,上面讲的就是请求的方法,下面讲讲请求头是什么?
![](http://static.oschina.net/uploads/img/201603/11172618_oEpC.png)
![](http://static.oschina.net/uploads/img/201603/11172618_qOaP.png)
![](http://static.oschina.net/uploads/img/201603/11172619_1oNj.png)
![](http://static.oschina.net/uploads/img/201603/11172619_t6nk.png)
![](http://static.oschina.net/uploads/img/201603/11172619_UrLG.png)
![](http://static.oschina.net/uploads/img/201603/11172619_tJbH.png)
![](http://static.oschina.net/uploads/img/201603/11172619_i02B.png)
![](http://static.oschina.net/uploads/img/201603/11172619_ctJL.png)
最后显示的是 cod=404表示出错了,然后我们把它改为GET请求,然后就成功了。
说明这个网址虽然 包头 信息告诉我们可以用GET和POST请求,但是通过POST给我们返回的是404的错误信息。
以上上面讲到的三点基础知识点 需要熟悉。