Swift之网络请求

在网络编程中,熟知的请求方式是GET、POST

这个周末在学习多线程下载时,原来还有HEAD请求,我真是井底之蛙。

HEAD请求,顾名思义,只获取服务器返回给我们的头部信息,其他内容信息不返回。

    /**
    使用HEAD方式请求服务器
    */
    func sendRequestWithHEAD() {
        let uri = "http://s1.music.126.net/download/osx/NeteaseMusic_1.1.1.320.dmg";
        let url = NSURL(string: uri)!;
        // 声明NSMutableURLRequest对象,便于设置请求属性
        let request = NSMutableURLRequest(URL: url);
        // 设置HEAD请求
        request.HTTPMethod = "HEAD";
        NSURLConnection(request: request, delegate: self);
    }

extension MultithreadController: NSURLConnectionDataDelegate {

    func connection(connection: NSURLConnection, didFailWithError error: NSError) {
        println("didFailWithError");
    }

    func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
        println("didReceiveResponse:\(response)");
    }

    func connection(connection: NSURLConnection, didReceiveData data: NSData) {
        println("didReceiveData----\(data.length)");
    }

    func connectionDidFinishLoading(connection: NSURLConnection) {
        println("connectionDidFinishLoading");
    }
}

下面是控制台的输出内容:

didReceiveResponse:<NSHTTPURLResponse: 0x7ff4584961a0> { URL: http://s1.music.126.net/download/osx/NeteaseMusic_1.1.1.320.dmg } { status code: 200, headers {
    "Accept-Ranges" = bytes;
    Age = 1;
    Connection = "keep-alive";
    "Content-Length" = 6369858;
    "Content-Type" = "application/x-apple-diskimage";
    Date = "Sat, 31 Jan 2015 09:38:43 GMT";
    "Last-Modified" = "Tue, 02 Dec 2014 12:22:49 GMT";
    P3P = "policyref=\"/w3c/p3p.xml\", CP=\"CUR ADM OUR NOR STA NID\"";
    Server = nginx;
    "X-Via" = "1.1 shhl152:10 (Cdn Cache Server V2.0)";
} }
connectionDidFinishLoading

重新学习一门编程语言的过程,也是在对以前编程过程的完善过程~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值