在网络编程中,熟知的请求方式是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
重新学习一门编程语言的过程,也是在对以前编程过程的完善过程~~