网络通信
2015-11-20 16:00
288 查看
用字符串来加载网页的html信息
读成二进制的文件
不过这样做一直会线程崩溃
以上这两种方式都是同步的,也就是主界面的UI是处于卡死状态的,用户是不能操作的,所以,这两种方法在操作本地的文件时,是可以用得,在操作网络数据时,不建议
应答头是http协议的数据
所以先定义
再将其地址传入
实质上错误已经在抛出异常时处理了
异步的处理,实质上是为了防止主界面线程UI的占用
import UIKit class ViewController: UIViewController { override func viewDidLoad() { var str = String() super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //用字符串来加载网页的html信息 do { str = try NSString(contentsOfURL:NSURL(string: "http://jikexueyuan.com")!, encoding: NSUTF8StringEncoding) as String } catch { print("加载失误") } print(str) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
读成二进制的文件
import UIKit class ViewController: UIViewController { var data = NSData() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //加载成二进制的数据 data = ((NSData(contentsOfURL: ((NSURL(string: "http://www.baidu.com")))!)!)) print(data) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
不过这样做一直会线程崩溃
以上这两种方式都是同步的,也就是主界面的UI是处于卡死状态的,用户是不能操作的,所以,这两种方法在操作本地的文件时,是可以用得,在操作网络数据时,不建议
import UIKit class ViewController: UIViewController { var data = NSData() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //用同步加载 send来加载网络上得数据,先不回应得到数据后的应答 do { var data = try NSURLConnection.sendSynchronousRequest(NSURLRequest(URL: NSURL(string: "http://www.baidu.com")!), returningResponse: nil) } catch { print("error") } //转化成字符串类型输出 原先是二进制的类型 print(NSString(data: data, encoding: NSUTF8StringEncoding)) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
应答头是http协议的数据
所以先定义
var resp:NSURLResponse?
再将其地址传入
do { var data = try NSURLConnection.sendSynchronousRequest(NSURLRequest(URL: NSURL(string: "http://www.baidu.com")!), returningResponse: &resp)
实质上错误已经在抛出异常时处理了
异步的处理,实质上是为了防止主界面线程UI的占用
import UIKit import SystemConfiguration class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //异步请求数据,是为了不影响用户的主界面。来下载数据 不能够影响主队列 NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: NSURL(string: "http://www.baidu.com")!), queue: NSOperationQueue()) { (resp:NSURLResponse?, data:NSData?, error:NSError?) -> Void in if let e = error { print("发生错误") }else { print(NSString(data: data! , encoding: NSUTF8StringEncoding)) } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
相关文章推荐
- 基于Nutch+Hadoop+Hbase+ElasticSearch的网络爬虫及搜索引擎
- AFNetWorking初探之AFHTTPRequestOperation(二)
- tcp 出现rst情况整理
- socket网络编程中read与recv区别
- android网络编程之——客户端上传信息的代码
- Nginx配置https访问
- 【转】Jmeter Http请求界面解释
- 虚拟机ping不通主机
- 关于 Xcode7网络请求的Http 协议改为 Https 协议的程序修改
- HTTP客户端服务端TCP连接内幕
- TCP连接探测中的Keepalive和心跳包
- ab和jmeter进行GET/POST压力测试的使用心得和比较
- [python&php 网络编程]设定并获取socket超时时间
- TCP的状态(SYN FIN ACK PSH RST URG)
- HTTP Method 之 Post VS. Get
- IIS7 IIS7.5 IIS8.5 HTTP 错误 500.19 – Internal Server Error解决方案小记
- http get with body data
- Windows Azure Virtual Machine (27) 使用psping工具,测试Azure VM网络连通性
- 网络爬虫雏形1号
- HttpClient4.3 使用经验(一) 简单使用