博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 开发笔记-NSURLConnection的使用
阅读量:7145 次
发布时间:2019-06-29

本文共 2762 字,大约阅读时间需要 9 分钟。

 通过NSURLConnection发送一个HTTP GET请求

//send a GET request to server with some params-(void)httpGetWithParams{    NSString *urlString = @"http://chaoyuan.sinaapp.com";    urlString = [urlString stringByAppendingString:@"?p=1059"];    NSURL *url = [NSURL URLWithString:urlString];    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];    [urlRequest setTimeoutInterval:30.0f];    [urlRequest setHTTPMethod:@"GET"];    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        if ([data length] > 0 && connectionError == nil) {            NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];            NSLog(@"HTML = %@",html);        }else if([data length] == 0 && connectionError == nil){            NSLog(@"nothing was download.");        }else if(connectionError != nil){            NSLog(@"Error happened = %@",connectionError);        }    }];}

通过NSURLConnection发送一个HTTP POST请求

//send a POST request to a server with some params-(void)httpPostWithParams{    NSString *urlAsString = @"http://chaoyuan.sinaapp.com";    urlAsString = [urlAsString stringByAppendingString:@"?param1=First"];    urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];    NSURL *url = [NSURL URLWithString:urlAsString];    NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2";    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];    [urlRequest setTimeoutInterval:30.0f];    [urlRequest setHTTPMethod:@"POST"];    [urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];    [urlRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    [NSURLConnection     sendAsynchronousRequest:urlRequest     queue:queue completionHandler:^(NSURLResponse *response, NSData *data,                                     NSError *error) {         if ([data length] >0 &&             error == nil){             NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"HTML = %@", html);             // 显示到UIWebView             //[self.bankWebView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];         }         else if ([data length] == 0 &&                  error == nil){             NSLog(@"Nothing was downloaded.");         }         else if (error != nil){             NSLog(@"Error happened = %@", error);         }     }];}

 

转载地址:http://otgrl.baihongyu.com/

你可能感兴趣的文章
《转》 Openstack Grizzly 指定 compute node 创建 instance
查看>>
[转]PhoneGap使用PushPlugin插件实现消息推送
查看>>
PHP传值与传址(引用)
查看>>
JSP简单练习-数组应用实例
查看>>
Directx11学习笔记【四】 封装一个简单的Dx11DemoBase
查看>>
DMA(STM32)
查看>>
最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
查看>>
Boost.Asio基础(三)
查看>>
【转载】学习新东西的唯一方法
查看>>
[转]Android dex分包方案
查看>>
关于Redis的启动过程
查看>>
Android 按二次后退键退出应用程序
查看>>
Springboot监控之一:SpringBoot四大神器之Actuator之2--springboot健康检查
查看>>
[唐诗]秋夜喜遇王处士-王绩
查看>>
一个简单多任务内核实例的分析【转】
查看>>
WPF 3D 小小小小引擎 - ·WPF 3D变换应用
查看>>
又一道简单题&&Ladygod(两道思维水题)
查看>>
golang笔记——函数与方法
查看>>
Linux LVM硬盘管理及LVM扩容
查看>>
针对某个数据库error做systemstate dump
查看>>