码迷,mamicode.com
首页 > 移动开发 > 详细

iOS网络篇4-利用NSURLConnection实现GET/POST/HEAD请求

时间:2015-01-26 19:11:17      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

iOS 开发中,我们可以利用NSURLConnection实现GET/POST请求

一、NSURLConnection三种请求方式

1.发送同步请求(返回NSData数据)

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

在当前线程执行,返回NSData数据

2.发送异步请求(利用block)

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSLog(@"返回数据:%@",data);

    }];

在异步线程执行,请求成功后,在block回调函数里拿到返回的数据体

3.发送异步请求(利用代理)

NSURLConnection *connection = [NSURLConnection alloc] init];

connection.delegate = self;

在异步线程执行,请求成功后,调用代理的相关方法拿到相应的数据,这种方法适合大文件分段下载。

/**1.接收到服务器的响应 */
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
/**2.接收到服务器的数据*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
/**3.服务器的数据接收完毕*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
/**4.请求错误*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}

 

二、GET请求

1.如果有参数,直接拼接在URL后面,用?隔开,且每个参数用&分开.

利用NSURLConnection不需要包含请求行和请求头,底层以及包装好,且默认就是get请求.我们只需加载指定URL资源,创建请求,发送请求即可。

示例程序:访问服务器登录界面

    //1.资源字符串
    NSString *urlStr = @"http://127.0.0.1/login.php?name=张三&password=1234";
    //如果字符串里面含有中文要进行转码
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //2.创建资源路径
    NSURL *url = [NSURL URLWithString:urlStr];
    //3.创建请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //4.发送请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%@",data);
    }];

二、post请求

利用NSURLConnection,由于默认是GET请求,所以要申明请求方法为POST,且需要封装请求体

    //1.资源字符串
    NSString *urlStr = @"http://127.0.0.1/login.php";
    //如果字符串里面含有中文要进行转码
    //urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    //2.设置请求路径
    NSURL *url = [NSURL URLWithString:urlStr];
    
    //3.创建请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 默认就是GET请求
    request.timeoutInterval = 5; // 设置请求超时
    request.HTTPMethod = @"POST"; // 设置为POST请求
//4.设置请求体 NSString *param = [NSString stringWithFormat:@"name=%@&password=%@",name, password];//name和password一般从输入框取得 request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; //5.发送请求 NSOperationQueue *queue = [NSOperationQueue mainQueue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 当请求结束的时候调用 NSLog(@"返回数据-%@",data); }];

三、head请求

其实head请求和get请求差不多,只不过是服务器只返回响应头,不返回具体数据,这可以帮助我们后面讲解下载文件时如何获得文件大小

NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/images/xhr.png"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"HEAD";
    NSURLResponse *response = nil;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

NSLog(@"文件长度-%lld",response.expectedContentLength;
}];

相比较而言,post请求比get请求安全,get请求所有数据暴露在URL中,post数据封装在请求体中, 不过通过fireFox也能查看到post的请求体,对于网页服务器开发,firefox是个利器,强烈推荐。

对于数据安全这块不用担心,后面会讲解如何对数据进行加密处理.

iOS网络篇4-利用NSURLConnection实现GET/POST/HEAD请求

标签:

原文地址:http://www.cnblogs.com/wc85328/p/4250918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!