同步Post方式
// MARK: - 同步Post方式 func synchronousPost() { //创建NSURL对象 var url:NSURL! = NSURL(string: "http://m.weather.com.cn/data/101010100.html") //创建请求对象 var request : NSMutableURLRequest = NSMutableURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: ) request.HTTPMethod = "POST"//设置请求方式为POST,默认为GET var str:String = "type=focus-c";//设置参数 var data:NSData = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)! request.HTTPBody = data; //响应对象 var response:NSURLResponse? //错误对象 var error:NSError? //发出请求 var received:NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error) if (error != nil) { println(error?.code) println(error?.description) }else { var jsonString = NSString(data: received!, encoding: NSUTF8StringEncoding) println(jsonString) } }