·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> iOS-Http:GET:POST

iOS-Http:GET:POST

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

一.概述

* HTTP/1.1协议共定义了8中请求方法:OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE, CONNECT.

* GET方法和POST是我们使用最频繁的网络请求方法。

* GET和POST在应用场合有什么区别呢?

* GET方法向指定资源发出请求,发送的消息显示的跟在URL后面,用户信息不安全,并且传送信息量有限。(如下所示,在请求中能看到用户名和密码)

   http://localhost:8080/logandreg/logreg?name=wyg&pwd=1992

* 如果仅仅是向服务器索要数据,没有参数,使用GET比较方便。(如下所示)

   http://www.baidu.com

* POST传送的信息量大,并且传送的信息是被隐藏的,传送信息比较安全,如果向服务器传送数据,建议使用POST.

二.GET请求网络数据(同步,异步)

* 如上所述,GET方法可以向指定资源发出请求,比如我们想再网络上请求一张图片在本地上显示,使用GET方法就非常的方便。

* GET请求分为同步请求和异步请求,一般情况下,为了良好的用户体验,我们都使用异步请求。

* GET同步请求一张网络图片(代码折叠)

1 NSURL *url = [NSURL URLWithString:@"http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"];
2 NSURLRequest *request = [NSURLRequest requestWithURL:url];
3 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
4  _iamgeview.image = [UIImage imageWithData:data];
同步获取网络图片

 *GET异步请求一张网络图片(iOS5.0)(代码折叠):

1 NSURL *url = [NSURL URLWithString:@"http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"];
2 NSURLRequest *request = [NSURLRequest requestWithURL:url];
3 NSOperationQueue *queue = [[NSOperationQueue alloc]init];
4 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
5  _iamgeview.image = [UIImage imageWithData:data];
6     }];
异步获取网络图片

  *GET异步请求(iOS2.0)(代码折叠)

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     NSURL *url = [NSURL URLWithString:@"http://img.ivsky.com/img/bizhi/slides/201508/18/september-012.jpg"];
 5     NSURLRequest *request = [NSURLRequest requestWithURL:url];
 6     [NSURLConnection connectionWithRequest:request delegate:self];
 7 }
 8 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
 9 {
10     NSLog(@"接收到响应");
11     _buffer = [[NSMutableData alloc]init];
12 }
13 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
14 {
15     NSLog(@"接收到数据");
16     [_buffer appendData:data];
17 }
18 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
19 {
20     NSLog(@"请求完成");
21     _iamgeview.image = [UIImage imageWithData:_buffer];
22 }
23 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)erro
24 {
25     NSLog(@"请求出错");
26 }
异步请求网络图片

 三.POST请求网络数据(以异步为例,同步的与GET类似)

* 传输信息安全性比GET高。

* 传输信息量比GET大。

* 代码中带有详细解释,代码如下:

 

- (IBAction)postRequest:(id)sender
{
    //明确请求的url
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/logandreg/logreg"];
    //创建请求(可变请求)
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //指定请求方式
    [request setHTTPMethod:@"POST"];
    //拼接参数内容
    NSString *body = @"name=wyg&pwd=1992";
    //请求数据放到请求的请求体中
    [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    //使用post发起异步请求
    [NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //接收到响应之后响应的方法
    _buffer = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //接收到数据之后响应的方法
    [_buffer appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //数据处理完成之后响应的方法
    NSString *str = [[NSString alloc]initWithData:_buffer encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //请求出错之后响应的方法(如断网,超时)
}