gpt4 book ai didi

swift - NSURLSession:无需身份验证即可 NSURLSession ( Swift )

转载 作者:行者123 更新时间:2023-11-30 13:53:27 24 4
gpt4 key购买 nike

我想要一个 API key 并验证用户身份。我在控制台中看到消息“ session URL 任务成功:HTTP 200”。不幸的是,我总是收到页面“{”消息“:”的消息。此请求“}”的授权已被拒绝。我花了几个小时寻找并找到了解决方案。我请求你的帮助。这是我的代码:

    let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()


let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)


let URL = NSURL(string: "https://api.testhomepage.com/api/contact")
let request = NSMutableURLRequest(URL: URL!)

// Headers

request.addValue("ARRAffinity=259dfjiehfs315d7249df8805c7895a98c4cbd3327ffdcb82aaa16317f2e6", forHTTPHeaderField: "Cookie")
request.addValue("Basic dWVzQGp1bGl0ZafdgfvcfgeDcyZS1hZWEyLTQzYmVmMjFhNDMxZQ==", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("api.testhomepage.com", forHTTPHeaderField: "Host")
request.addValue("//api.testhomepage.com/api/contact: POST", forHTTPHeaderField: "https")
request.HTTPMethod = "GET"
// JSON Body


/* Start a new Task */
let task = session.dataTaskWithRequest(request) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
if (error == nil) {
// Success
let statusCode = (response as! NSHTTPURLResponse).statusCode
print("URL Session Task Succeeded: HTTP \(statusCode)")
let myURLString = "https://api.testhomepage.com/api/contact"

if let myURL = NSURL(string: myURLString) {
var error: NSError?
let myhtml = try! NSString(contentsOfURL: myURL, encoding: NSUTF8StringEncoding)

if let error = error {
print("Error : \(error)")
} else {
print("HTML : \(myhtml)")
}
} else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
}

}
else {
// Failure
print("URL Session Task Failed: %@", error!.localizedDescription);
}
}

task.resume()

}

这是在控制台中:

URL session 任务成功:HTTP 200HTML : {"Message":"此请求的授权已被拒绝。"}

最佳答案

使用 request.setvalue("", forHTTPHeaderField: "") 而不是 request.addValue("", forHTTPHeaderField: "")

关于swift - NSURLSession:无需身份验证即可 NSURLSession ( Swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966263/

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