Swift:从Server Response获取HTTP代码

我在Swift中使用以下函数连接到服务器上的PHP脚本,然后处理我的请求: </ p>

  func pushtoserveronclick(){
var URL:NSURL = NSURL(string:“http://someurl.com/”)!
var request:NSMutableURLRequest = NSMutableURLRequest (URL:URL)
request.HTTPMethod =“POST”
var bodyData =“data = something&amp; version = \(appVersion)”
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest( 请求,队列:NSOperationQueue.mainQueue())
{
(响应,数据,错误)
println(响应)
}
}
</ code> </ pre>

< p>当我运行应用程序时,我将其作为响应返回:</ p>

 &lt; NSHTTPURLResponse:0x14e53170&gt;  {URL:http://someurl.com/} {status code:200,headers {
Connection =“Keep-Alive”;
“Content-Type”=“text / html”;
Date =“Fri,05 2014年12月06:23:23 GMT“;
”Keep-Alive“=”timeout = 2,max = 100“;
Server = Apache;
”Transfer-Encoding“= Identity;
</ code> < / pre>

我如何获得正在返回的状态代码</ strong>,以便我可以对其进行检查并判断它是否是连接问题?</ p>

</ div>

展开原文

原文

I'm using the following function in Swift to connect to a PHP Script on a server and then process my request:

func pushtoserveronclick() {
    var URL: NSURL = NSURL(string: "http://someurl.com/")!
    var request:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
    request.HTTPMethod = "POST"
    var bodyData = "data=something&version=\(appVersion)"
    request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
        {
            (response, data, error) in                       
            println(response)                
    }
 }

When I run the app I get this returned as the response:

<NSHTTPURLResponse: 0x14e53170> { URL: http://someurl.com/ } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Type" = "text/html";
Date = "Fri, 05 Dec 2014 06:23:23 GMT";
"Keep-Alive" = "timeout=2, max=100";
Server = Apache;
"Transfer-Encoding" = Identity;

How would I get the status code that is being returned so I can perform a check on it and tell whether or not it's a connection issue?

dongqingcheng2903
dongqingcheng2903 对不起,忘记了问题的背景。见答案。
接近 6 年之前 回复
dongmeiyi2266
dongmeiyi2266 是不是Obj-C?我对此很陌生
接近 6 年之前 回复
duanlang1531
duanlang1531 首先将其转换为NSHTTPURLResponse。[(NSHTTPURLResponse*)响应statusCode]
接近 6 年之前 回复
douzhi1937
douzhi1937 已经尝试过了。我得到'NSURLResponse'没有名为'statusCode'的成员
接近 6 年之前 回复
douqingji3026
douqingji3026 response.statusCode
接近 6 年之前 回复

1个回答



NSURLConnection.sendAsynchronousRequest </ code>在类型签名中使用 NSURLResponse </ code>,但只要 你通过HTTP连接,实例是(通常) NSHTTPURLResponse </ code>,它具有 statusCode </ code>。 </ p>

你可以这样检查和投射:</ p>

 如果让HTTPResponse =响应为?  NSHTTPURLResponse {
让statusCode = HTTPResponse.statusCode
}
</ code> </ pre>
</ div>

展开原文

原文

NSURLConnection.sendAsynchronousRequest uses NSURLResponse in the type signature, but as long as you're connecting over HTTP, the instance is (usually) NSHTTPURLResponse, which has the statusCode.

You can check and cast it this way:

if let HTTPResponse = response as? NSHTTPURLResponse {
    let statusCode = HTTPResponse.statusCode
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐