https - URLSession 或 Alamofire.Request 不适用于 URL 中的破折号?

标签 https alamofire xcode9 swift4 macos-high-sierra

我正在尝试向以下 URL 发出请求

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

此请求应该只返回纯文本:

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 10.13 的 macOS 项目(使用 Swift 4 和 Xcode 9)中使用代码,我会得到

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 中启用应用程序传输安全设置 --> 允许任意加载 --> 是,但这没有什么区别。

最佳答案

问题其实很简单:我只需要启用应用程序沙箱并检查传入和传出的网络连接。

关于https - URLSession 或 Alamofire.Request 不适用于 URL 中的破折号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46671635/

相关文章:

ios - Xcode 9 服务器重复配置文件

ssl - Internet Explorer 信任哪些证书颁发机构?

java - 如何在 Maven 的 settings.xml 中为 HTTP 和 HTTPS 配置代理服务器?

ios - 使用 Alamofire 4 将照片上传到 Swift 3 中的 Django?

ios - 从在线 JSON 文件加载选取器 View 选项

ios - 如何在Alamofire的上传方法中上传非字符串值的参数

ios - 核心数据 : annotation: Failed to load optimized model at path with Xcode 9 GM

xcode - Storyboard未在视觉上更新

python - 使用cherrypy进行HTTP和HTTPS

wordpress - 尽管正确安装了 SSL 证书,但 https 重定向到另一个页面