iOS开发ASIHttpRequest发送请求
#pragma mark - 同步请求 - (void)sync { // 1.创建请求对象 NSURL *url = [NSURL URLWithString:@"http://192.168.x.x:8080/login?username=123&pwd=123"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; request.timeOutSeconds = 10; // 2.发送请求 [request startSynchronous]; // 3.检测服务器返回的结果 if (request.error) { // 请求出错(比如超时) NSLog(@"请求出错---%@", request.error); } else { // 请求成功 NSLog(@"%d %@", request.responseStatusCode, request.responseStatusMessage); // NSLog(@"%d", request.responseData.length); NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"%@", dict); // NSLog(@"%@", request.responseString); } }
#pragma mark - 异步请求--设置代理 - (void)async { // 1.创建请求对象 NSURL *url = [NSURL URLWithString:@"http://192.168.x.x:8080/login?username=123&pwd=123"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; request.timeOutSeconds = 10; // NSURL *url2 = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/video"]; // ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2]; // 2.设置代理 request.delegate = self; // request2.delegate = self; [request setDidStartSelector:@selector(start)]; // 3.发送请求 [request startAsynchronous]; // [request2 startAsynchronous]; } - (void)start { NSLog(@"start--------"); } #pragma mark 代理方法 - (void)requestFinished:(ASIHTTPRequest *)request { NSLog(@"requestFinished----"); // request.responseData : 服务器返回的全部数据 } - (void)requestFailed:(ASIHTTPRequest *)request { NSLog(@"requestFailed----"); } - (void)requestStarted:(ASIHTTPRequest *)request { NSLog(@"requestStarted----"); } - (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data { NSLog(@"didReceiveData----"); }
#pragma mark - 异步请求--block - (void)async { // 1.创建请求对象 NSURL *url = [NSURL URLWithString:@"http://192.168.x.x:8080/login?username=123&pwd=123"]; self.request = [ASIHTTPRequest requestWithURL:url]; self.request.timeOutSeconds = 10; // 2.设置block [self.request setStartedBlock:^{ NSLog(@"setStartedBlock ----"); }]; [self.request setDataReceivedBlock:^(NSData *data) { NSLog(@"setDataReceivedBlock ----"); }]; [self.request setCompletionBlock:^{ NSLog(@"setCompletionBlock ----"); }]; // self.request.delegate = self; // 3.发送请求 [self.request startAsynchronous]; // NSURL *url2 = [NSURL URLWithString:@"http://192.168.x.x:8080/"]; // ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2]; // [request2 setCompletionBlock:^{ // // }]; // // [request2 setDataReceivedBlock:^(NSData *data) { // // }]; // [request2 startAsynchronous]; }
post请求
#pragma mark - post请求 - (void)post2 { // 1.创建请求对象 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/login"]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; // 2.添加请求参数(请求体中的参数) [request setPostValue:@"123" forKey:@"username"]; [request setPostValue:@"999" forKey:@"pwd"]; [request setCompletionBlock:^{ NSLog(@"---请求完毕"); }]; // 3.发送请求 [request startAsynchronous]; } - (void)post { // 1.创建请求对象 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/login"]; self.request = [ASIHTTPRequest requestWithURL:url]; self.request.timeOutSeconds = 10; // 设置请求方法 self.request.requestMethod = @"POST"; // 设置请求体 [self.request appendPostData:[@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding]]; // [self.request setPostLength:self.request.postBody.length]; // [self.request addRequestHeader:[NSString stringWithFormat:@"%d", self.request.postBody.length] value:@"Content-Length"]; // [self.request addRequestHeader:@"application/x-www-form-urlencoded" value:@"Content-Type"]; // 2.发送请求 [self.request startAsynchronous]; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。