iOS-->有關NSURLConnection

weixin_34292287發表於2016-07-19
1393984-1fba884b7fa25384.png
xcode.png

NSUrlConnection

使用NSURLConnection傳送請求的步驟:

  • ①設定請求路徑
  • ②建立請求物件(預設是GET請求,且已經預設包含了請求頭)
  • ③使用NSURLConnection傳送網路請求
  • ④接收到伺服器的響應後,解析響應體

使用NSURLConnection傳送Get請求

  • 1.傳送同步Get請求
-(void)sendSyncRequest{
    //確定請求路徑
    NSURL *url=[NSURL URLWithString:@"http://XXXX%B4%E5%81%A5&pwd=2uji&type=JSON"];
    //建立請求物件
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];

    NSURLResponse *response=nil;
    NSError *error=nil;
    //使用NSURLConnection傳送請求
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    //解析從伺服器返回的資料
    NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
    NSLog(@"%@",[NSThread currentThread]);
}
  • 2.傳送非同步Get請求
-(void)sendAsyncRequest{
    //確定請求路徑
    NSURL *url=[NSURL  URLWithString:@"http://XXXXX/login?username=XXX&pwd=XXX&type=JSON"];
    //建立請求物件
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
    //使用NSURLConnection傳送請求
   [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
       //解析從伺服器返回的資料
        NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
       //列印當前執行緒
       NSLog(@"%@",[NSThread currentThread]);

       //響應頭的真實型別為NSHTTPURLResponse
       NSHTTPURLResponse *response1=(NSHTTPURLResponse *)response;
       NSLog(@"%zd",response1.statusCode);
       NSLog(@"%@",response1.allHeaderFields);

    }];

}
  • 3.使用代理髮送請求
-(void)requestDelegate{
    //確定請求路徑
    NSURL *url=[NSURL  URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
    //建立請求物件
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];

//    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    //如果把最後一個引數設定為NO,就表示不立即傳送網路請求,而是在需要的時候呼叫Start方法來傳送
    //設定代理
    NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];
    [connection start];

}
//接收到伺服器響應的時候會呼叫此方法
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    self.data=[NSMutableData data];
}

//接收到從伺服器返回的資料的時候呼叫 (會多次呼叫)
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.data appendData:data];
}
//請求完成的時候呼叫該方法
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"%@",[[NSString alloc]initWithData:self.data encoding:NSUTF8StringEncoding]);
}

使用NSURLConnection傳送Post請求

-(void)post{
    //確定請求路徑
    NSURL *url=[NSURL URLWithString:@"http://x/login"];

    //建立可變的請求物件
    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];

    //設定請求方法為POST
    request.HTTPMethod=@"POST";
    //設定請求體
    request.HTTPBody=[@"username=xxx&pwd=xxxxx&type=JSON"dataUsingEncoding:NSUTF8StringEncoding];

    //設定超時時間
    request.timeoutInterval=15;

    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
    }];

URL中文轉碼處理

如果URL中包含中文,則在傳送請求的時候會出錯,為了解決這一問題,我們需要對含有中文的URL做一個轉碼處理:

 NSString *urlStr = @"http://120.25.226.186:32812/login2?username=哈哈&pwd=xxxx&type=JSON";

    NSLog(@"轉換前:%@",urlStr);
    //中文轉碼處理
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"轉換後:%@",urlStr);
    NSURL *url = [NSURL URLWithString:urlStr];

相關文章