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是个利器,强烈推荐。

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值