Synchronous Post Mode
1 //MARK:-Synchronous Post Mode2 func synchronouspost ()3 {4 //Create a Nsurl object5var url:nsurl! = Nsurl (string:"http://m.weather.com.cn/data/101010100.html")6 7 //Create Request Object8var request:nsmutableurlrequest = nsmutableurlrequest (Url:url, CachePolicy: Nsurlrequestcachepolicy.useprotocolcachepolicy, timeOutInterval:Ten)9 TenRequest. HttpMethod ="POST"//Set Request mode to post, default to get One Avar str:string ="type=focus-c";//Setting Parameters -var data:nsdata = str.datausingencoding (nsutf8stringencoding, Allowlossyconversion:true)! -Request. Httpbody =data; the - //Response Object -var response:nsurlresponse? - + //Error Object -var error:nserror? + A //make a request atvar received:nsdata? = Nsurlconnection.sendsynchronousrequest (Request, Returningresponse: &response, Error: &error) - - if(Error! =Nil) - { -println (error?). Code) -println (error?). Description) in}Else - { tovar jsonstring = NSString (data:received!, encoding:nsutf8stringencoding) + - println (jsonstring) the } *}
iOS Development--Network Programming Swift & (v) Synchronous post mode