gpt4 book ai didi

ios - AFNetworking 2 空 POST 响应但没有错误

转载 作者:行者123 更新时间:2023-11-29 12:26:45 25 4
gpt4 key购买 nike

这是一个非常棘手的问题:

我向服务器发送 POST 请求(登录)。

服务器会回答:

  • ok 状态码:200 + JSON 数据
  • 错误状态代码:401 + 纯文本

代码:

func login (id: String, password: String){

self.responseSerializer = AFJSONResponseSerializer()
self.responseSerializer.acceptableContentTypes = nil
self.responseSerializer.acceptableStatusCodes = NSIndexSet(index: 400)
//self.responseSerializer.acceptableStatusCodes = nil

var param = ["id": lbid, "password": password]

POST(APIURL.URL_LOGIN, parameters: param,

{ (operation : NSURLSessionDataTask!, response : AnyObject!) -> Void in
//var finalResponse : Dictionary = Dictionary<String, String>()
var tmp = response as String

self.defaults.setObject(tmp, forKey: "USERSSID")
self.defaults.setBool(true, forKey: "USERLOGGEDIN")

println("Success login")

}) { (operation : NSURLSessionDataTask!, error : NSError!) -> Void in

println(error)
}
}

它执行失败的博客,我得到这个错误:

    Code=-1011 "Request failed: no error (200)" UserInfo=0x7f9fa1534760 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7f9fa15791e0> { URL: https://************ } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 107;
"Content-Type" = "application/json";
Date = "Wed, 04 Mar 2015 21:47:51 GMT";
"Keep-Alive" = "timeout=7, max=150";
Server = Apache;
"Set-Cookie" = "SID=************; expires=Mon, 02-Mar-2020 21:47:51 GMT; path=/;domain=.*********";}},NSErrorFailingURLKey=https://***********,com.alamofire.serialization.response.error.data=< CORRECT POST BODY>, NSLocalizedDescription=Request failed: no error (200)}

如果我删除这段代码:

self.responseSerializer.acceptableStatusCodes = NSIndexSet(index: 400)

然后应用程序崩溃了。但是服务器响应状态代码为 200...

我不知道如何解决这个问题。你可以帮帮我吗?在这里,我得到了正确的 body 。但是为什么不在正常的成功博客中呢?

编辑:

    self.responseSerializer.acceptableStatusCodes = NSIndexSet(index: 200)

=> 应用崩溃

    self.responseSerializer.acceptableStatusCodes = nil

=> 应用崩溃

    self.responseSerializer.acceptableStatusCodes = NSIndexSet(index: 401)

=> 应用程序不会崩溃,但会执行失败 block 。错误消息中的状态代码为 200,错误数据包含正确的 POST 响应正文。=> 我可以从错误数据中提取消息...但这是一个如此简单的任务。它必须正常工作。

无法使用 Alamofire,因为我想使用 ssl 证书!

最终编辑:不要不知道为什么,但错误会自行消失。

最佳答案

如果您还没有这样做,请查看 Postman (谷歌浏览器应用程序)。这是调试 AFNetworking 问题的最佳方法,方法是模拟相同的请求并确保数据正确通过。很多次,我一直在解决一个问题,然后使用 Postman 并发现这是服务器正在做的事情。

关于ios - AFNetworking 2 空 POST 响应但没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com