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);
    

iOS中的iOS9之前的get和post请求_第1张图片

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

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]);
    }];


iOS中的iOS9之前的get和post请求_第2张图片

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) {
        
    }];


你可能感兴趣的:(IOS)