介绍5个简单的网络请求Demo。
1.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1. 创建一个网络请求
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2. 发送网络请求 网络请求是耗时操作.在子线程发送
//利用 NSUrlConnection 发送一个异步的网络请求,@param NSURLRequest 网络请求。
// queue: 操作队列!决定 网络请求完成之后的 Block(completionHandler) 回调在哪条线程执行!
// [NSOperationQueue mainQueue] 主线程
// completionHandler :Block 回调:网络请求完成之后,就会自动调用这个 Block!
<span style="color:#ff0000;">[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {</span>
// Block 中就是需要处理的数据!
// response : 服务器的响应.一般会返回一些对我们有用的数据信息.
// Content-Length:返回数据的长度,在文件下载的时候有用!Server = Apache; 服务器!
// data :二进制数据: 服务器返回的数据.(就是我们想要的内容)
// connectionError: 连接错误信息
// 根据返回的二进制数据,生成字符串! NSUTF8StringEncoding :编码方式:
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//在客户端直接打开一个网页!可以使用客户端浏览器: UIWebView
// 实例化一个客户端浏览器
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.bounds];
// 加载 html 字符串: baseURL:基准的地址。
[<span style="color:#ff0000;"><strong>web loadHTMLString</strong></span>:html baseURL:nil];
// 将浏览器加载在view 上。
[self.view addSubview:web];
}];
}
运行结果如下图:
2.
- (void)test1
{
// 1.创建网路请求
//@"http://www.baidu.com"域名是 IP 地址的一个速记符号!
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
// NSURLRequest 在网络请求中,内部已经把七层协议封装进去了!网络请求底层走的都是 Socket(套接字/插座) Socket :TCP/IP 协议
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2. 发送网络请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
// MIMEType :文件类型。"Content-Type" = "text/html;textEncodingName :编码方式 baseURL:基准地址
// 浏览器直接加载二进制数据这种网络请求方式没有适配客户端的数据!
[<span style="color:#ff0000;"><strong>web loadData:data</strong></span> MIMEType:nil textEncodingName:nil baseURL:url];
[self.view addSubview:web];
}];
}
运行结果如下图:
3.
- (void)test2
{
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 浏览器加载request的网络请求方式适配了客户端的数据!
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
<span style="color:#ff0000;"><strong>[web loadRequest:request];</strong></span>
[self.view addSubview:web];
}
运行结果如下图:
4.
- (void)test3
{
// 1.创建网路请求!
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
// 如果需要自己设置网络请求,就使用可变的网络请求。
<span style="color:#ff0000;">NSMutableURLRequest *request</span> = [NSMutableURLRequest requestWithURL:url];
// 告诉服务器,客户端的软件环境是 iPhone 手机。
// User-Agent :通过改变这个量,可以得到自己想要的一些界面,并且可以欺骗服务器,给我们返回不同的数据!
// 现在,一些大公司的页面都做成 响应式(自动适配PC 端 和 移动端) 的!
<span style="color:#3333ff;">// [request setValue:@"iPhone" forHTTPHeaderField:@"User-Agent"]; // 直接这样写,会出来一些简单的界面</span>
<span style="color:#ff0000;">[request setValue:@"iPhone AppleWebKit" forHTTPHeaderField:@"User-Agent"];</span>
// 2. 发送网络请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
<span style="color:#ff0000;">[web loadData:data</span> MIMEType:nil textEncodingName:nil baseURL:url];
[self.view addSubview:web];
}];
}
运行结果如下图:
5.
- (void)test4
{
// 1.创建网路请求!
NSString *urlString = @"http://127.0.0.1/login副本.html";
// 在 url 中,不能出现汉字,只能是 ASCII 吗! 如果 url 中出现了 汉字等特殊符号,必须使用百分号转译!否则,不会正常显示页面。
<span style="color:#ff0000;">urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];</span>
NSURL *url = [NSURL URLWithString:urlString];
// cachePolicy: 网络缓存策略:枚举常量
// timeoutInterval: 请求超时时间! 默认情况下,最大的请求超时时间是 1分钟! 在开发中,一般设 15S
// NSURLRequestUseProtocolCachePolicy = 0,
<span style="color:#ff0000;">NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:15];</span>
UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
<span style="color:#ff0000;">[web loadRequest:request];</span>
[self.view addSubview:web];
}