gpt4 book ai didi

ios - 复制匹配凭证时出错——Swift(REST API 调用)

转载 作者:可可西里 更新时间:2023-11-01 00:56:08 25 4
gpt4 key购买 nike

我正在尝试对通用设备中心进行 REST API 调用以打开开关。似乎通话正在进行,但我收到一条错误消息,提示我需要凭据,这是有道理的,因为需要凭据才能进入界面。但是我不确定如何进行这项工作。

我的代码如下

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBOutlet weak var lightOn: UIButton!
@IBAction func lightOn(_ sender: Any) {

guard let url = URL(string: "http://0.0.0.0/rest/nodes/ZW002_1/cmd/DFON") else { return }

let userCredential = URLCredential(user: "admin",
password: "admin",
persistence: .permanent)

URLCredentialStorage.shared.setDefaultCredential(userCredential, for: protectionSpace)

// create URL session ~ defaulted to GET

let session = URLSession.shared

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

// optional chaining to make sure value is inside returnables and not not

if let response = response {
print(response)
}

if let data = data {

// assuming the data coming back is Json -> transform bytes into readable json data

do {

let json = try JSONSerialization.jsonObject(with: data, options: [])

print(json)

} catch {

print("error")
}
}

}.resume() // if this is not called this block of code isnt executed

}

}

我试着在网上拼凑了几个示例,我看到的示例使用了 protectionSpace,但是当我使用它时,代码返回:

Use of unresolved identifier 'protectionSpace'

总的来说,每当我实际运行模拟器时,我都会得到这个确切的错误:

2017-12-26 13:28:58.656122-0600 hohmtest[6922:1000481] CredStore - performQuery - Error copying matching creds.  Error=-25300, query={
atyp = http;
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = http;
"r_Attributes" = 1;
sdmn = "/";
srvr = "192.168.1.73";
sync = syna;
}
<NSHTTPURLResponse: 0x60400042a3e0>
{ URL:
http://192.168.1.73/rest/nodes/ZW002_1/cmd/DON/ } { Status Code: 401,
Headers {
"Cache-Control" = (
"max-age=3600, must-revalidate"
);
Connection = (
"Keep-Alive"
);
"Content-Length" = (
0
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
EXT = (
"UCoS, UPnP/1.0, UDI/1.0"
);
"Last-Modified" = (
"Tue, 26 Dec 2017 11:26:15 GMT"
);
"Www-Authenticate" = (
"Basic realm=\"/\""
);
} }
error

最佳答案

这个解决方案对我有用。这就是我调用需要用户名和密码的 REST API 的方式。对于那些想知道的人,我将这段代码放在我的 IBAction 按钮中,除了制作按钮外不需要做任何其他事情。

let username = "admin"
let password = "admin"
let loginData = String(format: "%@:%@", username, password).data(using: String.Encoding.utf8)!
let base64LoginData = loginData.base64EncodedString()

// create the request
let url = URL(string: "http:/rest/nodes/ZW002_1/cmd/DFON")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("Basic \(base64LoginData)", forHTTPHeaderField: "Authorization")

//making the request
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse {
// check status code returned by the http server
print("status code = \(httpStatus.statusCode)")
// process result
}
}
task.resume()

********* 额外说明 *************

如果您没有用户名和密码,并且您正在尝试快速调用 REST API,这里有一些代码可以帮助您!两者都是 GET 请求!

@IBAction func onGetTapped(_ sender: Any) {

guard let url = URL(string: "https://jsonplaceholder.typicode.com/users") else { return }

// create URL session ~ defaulted to GET

let session = URLSession.shared

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

// optional chaining to make sure value is inside returnables and not not

if let response = response {
print(response)
}

if let data = data {

// assuming the data coming back is Json -> transform bytes into readable json data

do {

let json = try JSONSerialization.jsonObject(with: data, options: [])

print(json)

} catch {

print("error")
}
}

}.resume() // if this is not called this block of code isnt executed

}

关于ios - 复制匹配凭证时出错——Swift(REST API 调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47983026/

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