我已经构建了一个iOS应用程序,该应用程序调用服务器,该服务器仅接受来自列入白名单的IP的呼叫(客户端的安全要求)。他们现在设置了307个临时重定向。我已经将原始的根URL更改为新的重定向地址,现在出现401-未经授权的访问错误。除Urls外,代码中的其他所有内容均未更改。我已经使用Postman进行了测试,并且这些呼叫可以使用重定向地址。有谁知道是什么引起了这个问题?我怀疑HTTP标头已损坏,并且承载令牌在该过程中的某个位置丢失了。
这是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()
}