admin 管理员组

文章数量: 1184232

-( void )touchesBegan:( NSSet < UITouch *> *)touches withEvent:( UIEvent *)event{

[ self demo6 ];

}


-( void )demo6{

// 访问本地服务器

//    NSString *urlstr =@" ";

NSString *urlstr = @" 出庄记 (56).pages" ;

// url 中不能出现汉字,否则 url 值为空,如有汉字,需进行转换

urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding ];

NSURL *url = [ NSURL URLWithString :urlstr];

//cachePolicy: 网络缓存策略:玫举常量

//timeoutInterval: 请求超时时间 : 默认情况:最大 1 分钟,一般 15

NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL :url cachePolicy : 0 timeoutInterval : 15 ];

// 告诉服务器,客户端的软件环境

//    [request setValue:@"iphone" forHTTPHeaderField:@"User-Agent"]; // 简单界面

[request setValue : @"iphone appleWebKit" forHTTPHeaderField : @"User-Agent" ]; // demo3 界面一样

// 浏览器直接加载网络请求 , 适配屏幕

UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];

[web loadRequest :request];

[ self . view addSubview :web];

}


-( void )demo5{

// 访问优酷

//1. 创建一个可变的网络请求

NSURL *url = [ NSURL URLWithString : @"" ];

NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL :url];

// 告诉服务器,客户端的软件环境

//    [request setValue:@"iphone" forHTTPHeaderField:@"User-Agent"]; // 简单界面

[request setValue : @"iphone appleWebKit" forHTTPHeaderField : @"User-Agent" ]; // demo3 界面一样

// 浏览器直接加载网络请求 , 适配屏幕

UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];

[web loadRequest :request];

[ self . view addSubview :web];

}


-( void )demo4{

// 访问百度首页

//1. 创建一个可变的网络请求

NSURL *url = [ NSURL URLWithString : @"" ];

NSMutableURLRequest *request = [ NSMutableURLRequest requestWithURL :url];

// 告诉服务器,客户端的软件环境

//    [request setValue:@"iphone" forHTTPHeaderField:@"User-Agent"]; // 简单界面

[request setValue : @"iphone appleWebKit" forHTTPHeaderField : @"User-Agent" ]; // demo3 界面一样

//2. 发送网络请求

[ NSURLConnection sendAsynchronousRequest :request queue :[ NSOperationQueue mainQueue ] completionHandler :^( NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];


[web loadData :data MIMEType : nil textEncodingName : nil baseURL :url]; // 显示图片

[ self . view addSubview :web];

}];

}


-( void )demo3{

// 访问百度首页

//1. 创建一个网络请求

NSURL *url = [ NSURL URLWithString : @"" ];

NSURLRequest *request = [ NSURLRequest requestWithURL :url];

// 浏览器直接加载网络请求 , 适配屏幕

UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];


[web loadRequest :request];

[ self . view addSubview :web];

}


-( void )demo2{

// 打印沙盒路径

NSLog ( @"%@" , NSHomeDirectory ());

// 访问百度首页

//1. 创建一个网络请求

NSURL *url = [ NSURL URLWithString : @"" ];

NSURLRequest *request = [ NSURLRequest requestWithURL :url];

//2. 发送网络请求

[ NSURLConnection sendAsynchronousRequest :request queue :[ NSOperationQueue mainQueue ] completionHandler :^( NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

UIWebView *web = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];

//        [web loadData:data MIMEType:nil textEncodingName:nil baseURL:nil]; // 百度图片显示

[web loadData :data MIMEType : nil textEncodingName : nil baseURL :url]; // 显示图片

[ self . view addSubview :web];

}];

}


-( void )demo1{

// 访问百度首页

//1. 创建一个网络请求

NSURL *url = [ NSURL URLWithString : @"" ];

NSURLRequest *request = [ NSURLRequest requestWithURL :url];

//2. 发送网络请求

[ NSURLConnection sendAsynchronousRequest :request queue :[ NSOperationQueue mainQueue ] completionHandler :^( NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

// 网络响应

NSLog ( @"response %@" ,response);

// 网络返回的数据

NSString *html = [[ NSString alloc ] initWithData :data encoding : NSUTF8StringEncoding ];

NSLog ( @"data %@" ,html);

// 在本地保存百度首页

[data writeToFile : @"/Users/liaojianguo/Desktop/baidu.html" atomically : YES ];

// 网络错误

NSLog ( @"connectionError %@" ,connectionError);

}];

}

本文标签: 访问百度 创建一个 系统