NSUrl读取网络资源

来源:互联网 发布:网络直销流程五步 编辑:程序博客网 时间:2024/05/23 01:15
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];    NSLog(@"url的scheme为:%@",[url scheme]);    NSLog(@"url的host%@",[url host]);    NSLog(@"url的port%@",[url port]);    NSLog(@"url的path为%@",[url path]);    //使用URL对应的资源初始化NSString    NSString *baidu = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];//输出NSString 即可看到页面的源代码    NSLog(@"%@",baidu);}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

输出为:

2016-02-03 22:02:05.527 NSUrl[838:26688] url的scheme为:https2016-02-03 22:02:05.528 NSUrl[838:26688] url的hostwww.baidu.com2016-02-03 22:02:05.528 NSUrl[838:26688] url的port(null)2016-02-03 22:02:05.528 NSUrl[838:26688] url的path为2016-02-03 22:02:17.546 NSUrl[838:26688] <html><head><script>location.replace(location.href.replace("https://","http://"));</script></head><body><noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript></body></html>


3 0
原创粉丝点击