gpt4 book ai didi

ios - 调用 307 重定向服务器时的 401 响应

转载 作者:行者123 更新时间:2023-11-29 05:57:16 27 4
gpt4 key购买 nike

我构建了一个调用服务器的 iOS 应用程序,该服务器仅接受来自白名单 IP 的调用(客户端的安全要求)。他们现在已经设置了 307 临时重定向。我已将原始根 URL 更改为新的重定向地址,现在收到 401 - 未经授权的访问错误。除了 URL 之外,代码中的其他内容均未更改。我已经使用 Postman 进行了测试,并且调用可以使用重定向地址。有谁知道是什么导致了这个问题?我怀疑 HTTP header 已被损坏,并且不记名 token 在此过程中的某个地方丢失了。

这是 HTTP 响应:

<NSHTTPURLResponse: 0x600003315840> { URL: https://"API URL HERE" } { Status Code: 401, Headers {
"Access-Control-Allow-Origin" = (
"*"
);
"Content-Length" = (
0
);
Date = (
"Wed, 06 Mar 2019 13:50:30 GMT"
);
Server = (
""
);
"Www-Authenticate" = (
Bearer
);
"X-XSS-Protection" = (
"1; mode=block"
);

} }

这是 API 调用函数:

  func callAPI(jsonData: Data, childUrl: String, completionHandler: @escaping (_ success: Bool, _ data: Data, _ response: HTTPURLResponse) -> Void) {

let access_token = cache.access_token
let networkHeaders = [ "Content-Type": "application/json", "Authorization": access_token, "cache-control": "no-cache" ]
print(access_token)
let dataResponse = Data()
let urlResponse = HTTPURLResponse()
guard let url = URL(string: config.rootAPIUrl + childUrl) else { return }

var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = jsonData
request.allHTTPHeaderFields = networkHeaders


let session = URLSession.shared

session.dataTask(with: request) { data, response, error in
if error != nil {
DispatchQueue.main.async {
completionHandler(false, dataResponse, urlResponse)
}
}
if let response = response as? HTTPURLResponse, let data = data {
DispatchQueue.main.async {
completionHandler(true, data, response)
}
}
}.resume()
}

最佳答案

经过 Apple 的大量研究和帮助。该问题是由 header 中丢失“授权”值引起的。解决方案是实现重定向委托(delegate)并重新附加 header 值。这是工作代码

         func callAPI(jsonData: Data, childUrl: String, completionHandler: @escaping (_ success: Bool, _ data: Data, _ response: HTTPURLResponse) -> Void) {

self.jsonData = jsonData

let access_token = cache.access_token
let networkHeaders = [ "Content-Type": "application/json", "Authorization": access_token, "cache-control": "no-cache" ]
let dataResponse = Data()
let urlResponse = HTTPURLResponse()
guard let url = URL(string: config.rootAPIUrl + childUrl) else { return }

var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = jsonData
request.allHTTPHeaderFields = networkHeaders

let configuration = URLSessionConfiguration.default
var session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)

session.dataTask(with: request) { data, response, error in

if error != nil {
DispatchQueue.main.async {
completionHandler(false, dataResponse, urlResponse)
}
}
if let response = response as? HTTPURLResponse, let data = data {
DispatchQueue.main.async {
completionHandler(true, data, response)
}
}
}.resume()
}


func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {

let access_token = cache.access_token
let networkHeaders = [ "Content-Type": "application/json", "Authorization": access_token, "cache-control": "no-cache" ]

guard let url = request.url else { return }
guard let jsonData = self.jsonData else { return }

var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = jsonData
request.allHTTPHeaderFields = networkHeaders

completionHandler(request)
}

关于ios - 调用 307 重定向服务器时的 401 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55024711/

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