我编写了以下函数来建立与服务器的同步连接,发送请求并接收响应,这可以正常工作。
func connserv()
{
var request = NSMutableURLRequest(URL: NSURL(string: "https://144.1.1.45:8000")!)
//request.timeoutInterval = 10 // for implementing timeout
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var response: NSURLResponse?
var error: NSError?
println("Firing synchronous url connection......")
let urlData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)
if urlData == nil
{
println("Error happend timeout======\(error)!")
}
else
{
println("\(urlData!.length) bytes of data was returned")
println(response!);
println(NSString(data: urlData!, encoding: NSUTF8StringEncoding)!)
}
}
昨天我添加了以下代码行来实现连接超时,它昨天正常运行,但今天无效。我不知道出了什么问题。
request.timeoutInterval = 10
您能否告诉我如何在上述功能中实现连接超时,此功能还应尝试重新连接服务器三次。