//ViewController.swift
importUIKit
classViewController:UIViewController{
/*
URL:全球資源定位符
*/
/*
get請(qǐng)求/post請(qǐng)求:
(1)安全性:post安全性比較高
(2)get用于一般請(qǐng)求,涉及到用戶隱私數(shù)據(jù)的話采用post
*/
overridefuncviewDidLoad() {
super.viewDidLoad()
//post請(qǐng)求鏈接:
letpostUrlString ="http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?"
//post請(qǐng)求體:
letbodyString ="cid=213&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&date=20131129"
leturl =NSURL(string: postUrlString)
//創(chuàng)建一個(gè)可變的請(qǐng)求對(duì)象(必須是可變的,因?yàn)槲覀円O(shè)置它的請(qǐng)求方式和請(qǐng)求體)
varrequest =NSMutableURLRequest(URL: url!)
//設(shè)置請(qǐng)求方式
request.HTTPMethod="POST"
//將上面的請(qǐng)求參數(shù)序列化成NSData類型的數(shù)據(jù)
//4代表UTF-8編碼
letdata = bodyString.dataUsingEncoding(4)
//將data設(shè)置為請(qǐng)求對(duì)象的請(qǐng)求體
request.HTTPBody= data
//創(chuàng)建對(duì)話
letsession =NSURLSession(configuration:NSURLSessionConfiguration.defaultSessionConfiguration())
//發(fā)起任務(wù)
lettask = session.dataTaskWithRequest(request, completionHandler: { (data, url, error) ->Voidin
//請(qǐng)求成功進(jìn)行反序列化
letroot:AnyObject? =NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers, error:NSErrorPointer())
println(root)
})
task.resume()
}
funcgetRequest(){
leturlString ="http://bea.wufazhuce.com/OneForWeb/one/getC_N?strDate=2015-11-18&strRow=2&strMS=1"
//NSUELRequest:將網(wǎng)址對(duì)象包裝成一個(gè)請(qǐng)求對(duì)象
leturl =NSURL(string: urlString)
letrequest =NSURLRequest(URL: url!)
//創(chuàng)建對(duì)話
letconfiguration =NSURLSessionConfiguration.defaultSessionConfiguration()
letsession =NSURLSession(configuration: configuration)
//由session發(fā)起get請(qǐng)求
lettask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) ->Voidin
//請(qǐng)求完成回調(diào)的閉包
letrootDict:[String:AnyObject] =NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error:NSErrorPointer())as![String:AnyObject]
letresult = rootDict["result"]as!String
println(result)
})
//開始任務(wù)
task.resume()
}
//使用NSURL進(jìn)行get請(qǐng)求
funcgetNSURL(){
leturlString ="http://bea.wufazhuce.com/OneForWeb/one/getC_N?strDate=2015-11-18&strRow=2&strMS=1"
//請(qǐng)求對(duì)象NSURL:iOS中對(duì)全球資源定位符封裝的一個(gè)對(duì)象
//將上面字符串網(wǎng)址封裝成一個(gè)網(wǎng)址對(duì)象
leturl:NSURL=NSURL(string: urlString)!
//NSURLSession:網(wǎng)絡(luò)對(duì)話
//網(wǎng)絡(luò)對(duì)話的配置:涉及到網(wǎng)絡(luò)任務(wù)是否在后臺(tái)執(zhí)行耗式,應(yīng)用程序休眠的時(shí)候網(wǎng)絡(luò)任務(wù)是否要繼續(xù),一般使用默認(rèn)的配置就足夠了
letconfiguration =NSURLSessionConfiguration.defaultSessionConfiguration()
//創(chuàng)建一個(gè)對(duì)話
letsession =NSURLSession(configuration: configuration)
//發(fā)起請(qǐng)求任務(wù)(data:請(qǐng)求成功獲取到的數(shù)據(jù),urlResponse:請(qǐng)求頭,error:請(qǐng)求失敗的錯(cuò)誤信息)
lettask = session.dataTaskWithURL(url, completionHandler: { (data, urlResponse, error) ->Voidin
//將獲取的數(shù)據(jù)進(jìn)行反序列成根節(jié)點(diǎn)
letrootDict:[String:AnyObject] =NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error:NSErrorPointer())as![String:AnyObject]
letresult = rootDict["result"]as!String
println(result)
iferror !=nil{
println("網(wǎng)絡(luò)出錯(cuò)")
}
})
//開始任務(wù)
task.resume()
}
overridefuncdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}