发送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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红米4x开不了机怎么办 鱼身上有红斑像出血了怎么办 草鱼身上有红斑像出血了怎么办 宝宝屁眼红的破皮了怎么办 孩子身上起红疙瘩很痒怎么办 久而不射,但软了怎么办 盆底综合肌力1级怎么办 头发掉的厉害怎么办吃什么好 给蜂蛰了肿了痒怎么办 小米手环2没电了怎么办 小米手环2不亮了怎么办 红米3s无限重启怎么办 乐视手机1s卡顿怎么办 老公出轨了怎么办你会选择离婚吗 c盘和d盘换换了怎么办 晚上2点到3点醒怎么办 红米3s变砖了怎么办 6s锁屏密码忘了怎么办 怀孕9个月了胃疼怎么办 怀孕6个月了胃疼怎么办 孕妇胃疼怎么办4个月了 25岁欠了5万块钱怎么办 感冒嗓子疼怎么办最简单的方法 和老婆离婚了我的心好痛怎么办 4s店不给退定金怎么办 教你闪腰了后该怎么办 coolpad酷派手机开不了机怎么办 苹果5s黑屏开不了机怎么办 苹果4s的屏坏了怎么办 苹果6手机充电口接触不良怎么办 5s用久了卡顿怎么办 孕妇血糖高怎么办什么方法降最好 脚砸了肿了紫了怎么办 我想在淘宝上卖东西该怎么办 苹果手机4s开不了机怎么办 冒险岛s前出2条怎么办 狗狗又吐又拉血怎么办 小孩上网成瘾怎么办父母要怎么做 一只眼睛大一只眼睛小怎么办 带近视镜时间长了眼睛变形怎么办 联通卡2g换4g卡怎么办