您的位置:首页 > 理论基础 > 计算机网络

NSUrl读取网络资源

2016-02-03 22:07 323 查看
#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为:https
2016-02-03 22:02:05.528 NSUrl[838:26688] url的hostwww.baidu.com
2016-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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: