IOS学习 网络 User-Agent 换行/r/n最安全 url汉字转换

-(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);

    }];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值