发送HTTP网络请求的方法
来源:互联网 发布:环境大数据 编辑:程序博客网 时间:2024/04/28 03:49
NSURLConntion GET请求
NSURLConnction发送请求的两种方式
1.同步请求:sendasync(数据一次性返回)
2.异步请求:1>sendAsync(数据一次性返回) 2>delegate(数据多次片段返回)
代理的三种请求方法
1. 》connection
2. 》initWith—
3. 》initWith 有startImmediately参数
1.发送同步请求
//1.获取发送网路请求的路径 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=456&type=JSON"]; //2.创建请求对象 //无需写请求体(系统自配) NSURLRequest *request = [NSURLRequest requestWithURL:url]; //3.发送请求 利用NSURLConnection发送请求 /** *第一个参数:请求对象 *第二个参数:响应头 传地址 *第三个参数:错误信息 **/ //NSURLResponse *response = nil; //响应的真实类型 利用属性读取其内部的信息 NSHTTPURLResponse *response = nil; //阻塞式(同步)的方法 卡主线程 NSError *error = nil; //此时的data是一次性接收到的 数据大时内存会爆掉 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; //4.解析 data--->string NSLog(@"------%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); NSLog(@"------%ld-----%@",response.statusCode,error.userInfo);
2.
2.1发送异步请求
//1.获取发送网路请求的路径 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"]; //2.创建请求对象 //无需写请求体(系统自配) NSURLRequest *request = [NSURLRequest requestWithURL:url]; //3.发送请求 /** *第一个参数:请求对象 *第二个参数:队列 决定代码块completionHandler的调用线程 *第三个参数:completionHandler请求完成(成功|失败)的时候调用 response 为响应头的参数 NSData 为响应体的参数 **/ //此时的data是一次性接收到的 数据大时内存会爆掉 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { NSString *userInfo = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //强转respose NSHTTPURLResponse *http = (NSURLResponse *)response; NSLog(@"---useInfo:%@----status:%ld",userInfo,http.statusCode); }];
2.2发送异步代理网络请求
//异步代理网络请求- (void)delegate{ //1.确定请求路径 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]; //2.创建请求对象 NSURLRequest *requst = [NSURLRequest requestWithURL:url]; //3.发送请求 //3.1第一中设置代理 发送请求 //[NSURLConnection connectionWithRequest:requst delegate:self]; //3.2第二种设置代理 发送请求 //[[NSURLConnection alloc] initWithRequest:requst delegate:self]; //3.3第三种设置代理 并不会发送请求 (startImmediately == YES 会发送)设置NO时要发送请求 必须要手动开启 //NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requst delegate:self startImmediately:YES]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requst delegate:self startImmediately:NO]; //调用开始方法 发送请求 [connection start]; //调用cance取消请求 //[connection cancel]; NSLog(@"****************");}#pragma mark-----#pragma NSURLConnectionDataDelegate//1.当接收到服务器响应的时候调用- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"%s",__func__);}//2.接收到服务器的数据时候调用 可能调用多次(数据较大,一点一点获取的)//此时的data是多次接收到的-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ NSLog(@"%s",__func__); //拼接数据 [self.resultDara appendData:data];}//3.当请求失败时调用- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"%s",__func__);}//4.请求结束时调用- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"%s",__func__); //解析数据 NSLog(@"---%@",[[NSString alloc] initWithData:self.resultDara encoding:NSUTF8StringEncoding]);}
#NSURLConnection POST请求
//1.获取发送路径 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"]; //2.创建请求对象 要加请求头 所以要创建可变请求对象 /** *请求对象包括 :1.NSURL 2。请求头 系统默认 也可自定义 3.请求体 GET无 POST自写 **/ NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:url]; //3.需要手动更改请求方法 POST必须大写 request.HTTPMethod = @"POST"; //设置属性 请求超时 request.timeoutInterval = 10; //也可设置请求头 //[request setValue:@"ios.1" forHTTPHeaderField:@"User-Agent"]; //4.设置请求提 request.HTTPBody = [@"username=520it&pwd=123&type=JSON" dataUsingEncoding:NSUTF8StringEncoding]; //5.发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { //6.解析数据data -- 》字符串 NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]); }];
0 0
- 发送HTTP网络请求的方法
- java网络编程:URLConnection发送HTTP请求的方法
- Android的http网络请求和发送
- 发送Http请求(POST GET)的方法
- 发送Http请求(POST GET)的方法
- 发送Http请求(POST GET)的方法
- 发送Http请求(POST GET)的方法
- 发送Http请求(POST GET)的方法
- 发送Http请求(POST GET)的方法
- 发送HTTP请求的方法:GET & POST
- Android网络:发送http请求
- 从servlet发送http请求进入http页面的方法
- OC网络:HTTP请求的常见方法
- java常用的发送http请求的工具方法
- [IPhone开发]发送Http请求(POST GET)的方法
- [IPhone开发]发送Http请求(POST GET)的方法
- [IPhone开发]发送Http请求(POST GET)的方法 .
- 使用CURL来发送HTTP请求的方法
- 使用fastjson 和 jackson的一些方法
- 仿微信底部导航栏您真的会了吗?
- 架构之美 ----- Dubbo 框架
- Android 之Map容器替换 SparseArray,ArrayMap,ArraySet
- Codeforces Round #382 (Div. 1)
- 发送HTTP网络请求的方法
- Android事件触发EditText焦点和自动弹出键盘
- 百度地图4.1_1开发教程(8)为marker加上备注
- React Native 之 View使用
- Android中汉字和16进制数的转化
- 欧拉计划001:3的倍数和5的倍数
- 《领测老贺聊软件测试》软件测试自媒体频道正式发布
- linux操作系统下解压rar
- Eclipse 的单步调试