URLSession或Alamofire.Request不能与URL中的破折号一起使用?

时间:2017-10-10 16:32:14

标签: https alamofire xcode9 swift4 macos-high-sierra

我正在尝试向以下网址发送请求

https://cactus.nci.nih.gov/chemical/structure/530-62-1/smiles

此请求应返回纯文本:

Clc1ccc(C=O)cc1

在像这样的游乐场中使用代码非常合适:

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,我得到了

  

请求的网址是:   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()失败   path:/ var / run / mDNSResponder Socket:11 Err:-1 Errno:1 操作不   允许 2017-10-10 17:23:08.476341 + 0100 AimieSmiles [18012:3495056]   [] nw_resolver_create_dns_service_locked   DNSServiceCreateDelegateConnection失败: ServiceNotRunning ( - 65563)   2017-10-10 17:23:08.476719 + 0100 AimieSmiles [18012:3495056] TIC TCP   Conn失败[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
    }

我收到以下错误:

  

请求:   可选(https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles)   响应:nil结果:FAILURE错误:可选(错误   Domain = NSURLErrorDomain Code = -1003“具有指定的服务器   无法找到主机名。“   UserInfo = {NSUnderlyingError = 0x60c000450800 {错误   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 = 具有指定主机名的服务器无法   找到。})

从错误中查看URL无效或无法从macOS应用程序识别?奇怪的是,它在游乐场中工作得很好......

我尝试启用应用传输安全设置 - >允许任意负载 - >在plist中是,但这没有任何区别。

1 个答案:

答案 0 :(得分:3)

问题变得微不足道:我只需要启用App Sandbox并检查传入和传出的网络连接。