1 (1).使用同步方法发送get请求(不常用) 2 /** 发送get消息 */ 3 - (void) testGet { 4 NSString *requestStr = [NSString stringWithFormat:@"http://192.168.0.21:8080/MyTestServer/login?user=%@&passWord=%@", self.userField.text, self.passwordField.text]; 5 6 NSURL *url = [NSURL URLWithString:requestStr]; 7 8 // 默认就是get请求 9 NSURLRequest *request = [NSURLRequest requestWithURL:url];10 11 // 使用同步方法发送请求12 [self sendSynRequest:request];13 }14 15 /** 同步发送请求 */16 - (void) sendSynRequest:(NSURLRequest *) request {17 // 同步发送信息18 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];19 20 [self dealWithResponseData:data];21 }22 23 /** 处理返回数据 */24 - (void) dealWithResponseData:(NSData *) data {25 // 解析数据26 if (data) { // 得到返回数据27 // 解除屏幕锁28 [MBProgressHUD hideHUD];29 30 // 解析json数据31 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];32 33 // 处理返回的数据34 NSString *result = dict[@"success"];35 if (result) {36 [MBProgressHUD showSuccess:result];37 } else {38 result = dict[@"error"];39 if (result) {40 [MBProgressHUD showError:result];41 }42 }43 } else {44 [MBProgressHUD showError:@"网络繁忙,请稍后再试~"];45 }46 }
1 /** 异步发送请求 */2 - (void) sendAsynRequest:(NSURLRequest *) request {3 NSOperationQueue *queue = [NSOperationQueue mainQueue];4 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {5 6 [self dealWithResponseData:data];7 }];8 }
1 @interface ViewController () <NSURLConnectionDataDelegate>
1 /** 使用start & 代理发送、处理异步请求 */2 - (void) sendAsynRequestWithDelegate:(NSURLRequest *) request {3 NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];4 [connection start];5 }
1 #pragma mark - NSURLConnectionDataDelegate 代理方法 2 /** 收到服务器回应 */ 3 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 4 NSLog(@"didReceiveResponse"); 5 self.data = [NSMutableData data]; 6 } 7 8 /** 接收到的数据,会调用多次,数据被分割接收 */ 9 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {10 NSLog(@"didReceiveData");11 [self.data appendData:data];12 }13 14 /** 接收数据完毕 */15 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {16 NSLog(@"connectionDidFinishLoading");17 [self dealWithResponseData:self.data];18 }
1 #pragma mark - post 2 - (void) testPost { 3 NSString *requestStr = [NSString stringWithFormat:@"http://192.168.0.21:8080/MyTestServer/login"]; 4 NSURL *url = [NSURL URLWithString:requestStr]; 5 6 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 7 request.timeoutInterval = 5; 8 9 // 设置为post方式请求10 request.HTTPMethod = @"POST";11 12 // 设置请求头13 [request setValue:@"ios" forHTTPHeaderField:@"User-Agent"];14 15 // 设置请求体16 NSString *param = [NSString stringWithFormat:@"user=%@&password=%@", self.userField.text, self.passwordField.text];17 request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];18 19 // 发送请求20 // 使用主线程来处理UI刷新21 NSOperationQueue *queue = [NSOperationQueue mainQueue];22 [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {23 [self dealWithResponseData:data];24 }];25 26 }
1 // 使用可变request2 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];3 // 设置请求超时时间4 request.timeoutInterval = 5;
1 NSString *requestStr = [NSString stringWithFormat:@"http://192.168.0.21:8080/MyTestServer/login?user=%@&password=%@", self.userField.text, self.passwordField.text];2 3 // 由于url不能传送中文,所以需要转码4 requestStr = [requestStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
新闻热点
疑难解答