iOS中的iOS9之前的get和post请求

网络请求

在前面说过网络的一些概念和本地服务器的搭建可以点击这里查看

get请求
get同步请求
/创建URL
    
    NSURL *url = [NSURL URLWithString:@"http://localhost/iOS/demo.json"];
    
    //创建请求
    NSURLRequest *re = [[NSURLRequest alloc] initWithURL:url];
    
    //发送请求
    NSURLResponse *responce = nil;
   //这个方法在iOS9之后废弃了
   NSData *data =  [NSURLConnection sendSynchronousRequest:re returningResponse:&responce error:nil];

    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"%@",dataStr);
    

在这里插入图片描述

这个操作是同步请求 主线程耗时 会造成卡顿的效果

get异步
  //创建URL
    
    NSURL *url = [NSURL URLWithString:@"http://localhost/iOS/demo.json"];
    
    //创建请求
      /*
     
     NSURLRequestUseProtocolCachePolicy = 0, 默认缓存策略
     
     NSURLRequestReloadIgnoringLocalCacheData = 1, 忽略本地缓存,直接加载网络数据
     NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4, // Unimplemented
     
     
     NSURLRequestReturnCacheDataElseLoad = 2,返回缓存数据,如果没有c缓存从网络上加载
     NSURLRequestReturnCacheDataDontLoad = 3, d返回缓存数据,如果没有返回nil
     
     NSURLRequestReloadRevalidatingCacheData = 5,
     
     */
    NSURLRequest *quest = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:15];
    
    //发送请求
    NSURLResponse *responce = nil;
   //创建一个队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
   [NSURLConnection sendAsynchronousRequest:quest queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
       
       NSLog(@"%@---\n--%@---\n---%@",[NSThread currentThread],responce,[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];


在这里插入图片描述

get请求NSURLConnection代理方法
- (void)addDelegateRequst{
 /创建URL
    
    NSURL *url = [NSURL URLWithString:@"http://localhost/iOS/demo.json"];
    
    //创建请求
    NSURLRequest *quest = [[NSURLRequest alloc] initWithURL:url];
    
    //发送请求

    [NSURLConnection connectionWithRequest:quest delegate:self];

}

//代理方法
//收到响应的时候执行
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    
}


//接受到数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    
}


//失败的时候
- (void)connection:(NSURLConnection *)connection didFailWithError:(nonnull NSError *)error{
    
}


//已经完成加载的时候
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
}

POST异步请求
//创建URL
    
    NSURL *url = [NSURL URLWithString:@"http://localhost"];
    
    //创建可变的网络请求
    
    NSMutableURLRequest *quest = [[NSMutableURLRequest alloc] initWithURL:url];
    //设置方式
    quest.HTTPMethod = @"POST";
    //请求超时的时间
    quest.timeoutInterval = 15;
 //设置请求头信息
    [quest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSString *body = @"userName=cc&psd=123";
    //转化 请求体
    quest.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
    
    
    [NSURLConnection sendAsynchronousRequest:quest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
    }];


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值