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

    [selfdemo6];

}


-(void)demo6{

    //访问本地服务器

//    NSString *urlstr =@"http://127.0.0.1/max/chuzhuangji.pages";

    NSString *urlstr =@"http://127.0.0.1/max/出庄记(56).pages";

    

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

    urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:urlstr];

    

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

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

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlcachePolicy:0timeoutInterval: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:@"http://v.youku.com/v_show/id_XMTUzMDE5NDM3Mg==.html?f=27048390"];

    

    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:@"http://www.baidu.com"];

    

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

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

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

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

    

    //2.发送网络请求

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

        

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


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

        

        [self.view addSubview:web];

    }];

}


-(void)demo3{

    //访问百度首页

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

    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];

    

    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:@"http://www.baidu.com"];

    

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    

    //2.发送网络请求

    [NSURLConnection sendAsynchronousRequest:requestqueue:[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:dataMIMEType:niltextEncodingName:nilbaseURL:url];//显示图片

        

        [self.view addSubview:web];

    }];

}


-(void)demo1{

    //访问百度首页

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

    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];

    

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    

    //2.发送网络请求

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

        //网络响应

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

        

        //网络返回的数据

        NSString *html = [[NSString alloc]initWithData:dataencoding:NSUTF8StringEncoding];

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

        

        //在本地保存百度首页

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

        

        //网络错误

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

    }];

}


Logo

Agent 垂直技术社区,欢迎活跃、内容共建,欢迎商务合作。wx: diudiu5555

更多推荐