URLSession 或 Alamofire.Request 在 URL 中不能使用破折号?
URLSession or Alamofire.Request don't work with dashes in URL?
我正在尝试向以下 URL
发出请求
https://cactus.nci.nih.gov/chemical/structure/530-62-1/smiles
此请求应该只是 return 纯文本:
Clc1ccc(C=O)cc1
像这样在 playground 中使用代码工作得很好:
var cas = "530-62-1"
let url = URL(string: "https://cactus.nci.nih.gov/chemical/structure/\(cas)/smiles")
var result = ""
let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
//print(response!)
result = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
print(result)
}
task.resume()
但是如果我尝试在我的 macOS 项目中使用代码(使用 Swift 4 和 Xcode 9)针对 macOS 10.13 我得到
URL for request is:
https://cactus.nci.nih.gov/chemical/structure/104-88-1/smiles
2017-10-10 17:23:08.475739+0100 AimieSmiles[18012:3495056]
dnssd_clientstub ConnectToServer: connect() failed
path:/var/run/mDNSResponder Socket:11 Err:-1 Errno:1 Operation not
permitted 2017-10-10 17:23:08.476341+0100 AimieSmiles[18012:3495056]
[] nw_resolver_create_dns_service_locked
DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563)
2017-10-10 17:23:08.476719+0100 AimieSmiles[18012:3495056] TIC TCP
Conn Failed [1:0x60c000162dc0]: 10:-72000 Err(-65563)
如果我尝试使用 Alamofire(我只是想检查一下是否有可能,使用如下代码:
func alamoRequest(cas: String) -> String {
var smilesResult = ""
var stringURL = "https://cactus.nci.nih.gov/chemical/structure/\(cas)/smiles"
var url = URL(string: stringURL)!
Alamofire.request(url).responseString { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
print("Error: \(String(describing: response.error))")
if let result = response.result.value {
smilesResult = result
print("Result: \(result)") // serialized json response
} else if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
smilesResult = utf8Text
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}
return smilesResult
}
我收到以下错误:
Request:
Optional(https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles)
Response: nil Result: FAILURE Error: Optional(Error
Domain=NSURLErrorDomain Code=-1003 "A server with the specified
hostname could not be found."
UserInfo={NSUnderlyingError=0x60c000450800 {Error
Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)"
UserInfo={_kCFStreamErrorCodeKey=-72000,
_kCFStreamErrorDomainKey=10}}, NSErrorFailingURLStringKey=https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles,
NSErrorFailingURLKey=https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles,
_kCFStreamErrorDomainKey=10, _kCFStreamErrorCodeKey=-72000, NSLocalizedDescription=A server with the specified hostname could not
be found.})
从错误中可以看出 URL 无效,或者无法被 macOS 应用程序识别?奇怪的是,它在 Playground 上运行良好...
我尝试在 plist 中启用 App Transport Security Settings --> Allow Arbitrary Loads --> YES 但这并没有什么不同。
问题原来是微不足道的:我只需要启用 App Sandbox 并检查传入和传出的网络连接。
我正在尝试向以下 URL
发出请求https://cactus.nci.nih.gov/chemical/structure/530-62-1/smiles
此请求应该只是 return 纯文本:
Clc1ccc(C=O)cc1
像这样在 playground 中使用代码工作得很好:
var cas = "530-62-1"
let url = URL(string: "https://cactus.nci.nih.gov/chemical/structure/\(cas)/smiles")
var result = ""
let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
//print(response!)
result = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
print(result)
}
task.resume()
但是如果我尝试在我的 macOS 项目中使用代码(使用 Swift 4 和 Xcode 9)针对 macOS 10.13 我得到
URL for request is: https://cactus.nci.nih.gov/chemical/structure/104-88-1/smiles 2017-10-10 17:23:08.475739+0100 AimieSmiles[18012:3495056] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:11 Err:-1 Errno:1 Operation not permitted 2017-10-10 17:23:08.476341+0100 AimieSmiles[18012:3495056] [] nw_resolver_create_dns_service_locked DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563) 2017-10-10 17:23:08.476719+0100 AimieSmiles[18012:3495056] TIC TCP Conn Failed [1:0x60c000162dc0]: 10:-72000 Err(-65563)
如果我尝试使用 Alamofire(我只是想检查一下是否有可能,使用如下代码:
func alamoRequest(cas: String) -> String {
var smilesResult = ""
var stringURL = "https://cactus.nci.nih.gov/chemical/structure/\(cas)/smiles"
var url = URL(string: stringURL)!
Alamofire.request(url).responseString { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
print("Error: \(String(describing: response.error))")
if let result = response.result.value {
smilesResult = result
print("Result: \(result)") // serialized json response
} else if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
smilesResult = utf8Text
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}
return smilesResult
}
我收到以下错误:
Request: Optional(https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles) Response: nil Result: FAILURE Error: Optional(Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x60c000450800 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, NSErrorFailingURLStringKey=https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles, NSErrorFailingURLKey=https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles, _kCFStreamErrorDomainKey=10, _kCFStreamErrorCodeKey=-72000, NSLocalizedDescription=A server with the specified hostname could not be found.})
从错误中可以看出 URL 无效,或者无法被 macOS 应用程序识别?奇怪的是,它在 Playground 上运行良好...
我尝试在 plist 中启用 App Transport Security Settings --> Allow Arbitrary Loads --> YES 但这并没有什么不同。
问题原来是微不足道的:我只需要启用 App Sandbox 并检查传入和传出的网络连接。