【问题标题】:Error Domain=NSURLErrorDomain Code=-1005 curl request to Uber-api错误域=NSURLErrorDomain 代码=-1005 对 Uber-api 的 curl 请求
【发布时间】:2016-05-14 12:10:55
【问题描述】:

我正在使用 swift 编写一个应用程序并尝试集成 Uber api。 我想在终端中执行这个非常简单(!?)的请求:

curl -X GET -G 'https://sandbox-api.uber.com/v1/products' -d server_token=*************************************** -d latitude=21.3088619 -d longitude=-157.8086674

为此,我尝试了以下代码:

    func performPostUberRequest() {
        let url: NSURL = NSURL(string: "https://sandbox-api.uber.com/v1/products")
        print("performPostUberRequest Ignited")
        let params:[String: AnyObject] = ["server_token" : "***************************************", "latitude" : "21.3088621", "longitude" : "-157.8086632"]
        let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "GET"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        do {
            try request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
        } catch {

        }
        print("performPostUberRequest ongoing")
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){

            response, data, error in
            if let error = error {
                print("error: ")
                print(error)
                print("data: ")
                print(data)
                print("response: ")
                print(response)
            } else if data != nil {
                do {
                    let json: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as! NSDictionary
                    print("here comes JASON: ")
                    print(json)

                } catch {
                    print("Epic Fail")
                }
            }
        }
    }

但无论我尝试 3-4 天,现在都会给我完全相同的答案:

performPostUberRequest Ignited
performPostUberRequest ongoing
error: 
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/products, _kCFStreamErrorCodeKey=-4, NSErrorFailingURLKey=https://sandbox-api.uber.com/v1/products, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x16da79f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=-4, NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/products, NSErrorFailingURLKey=https://sandbox-api.uber.com/v1/products, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=4}}}
data: 
nil
response: 
nil

编辑:

我下载了 Charles,它给了我以下屏幕:

令我困扰的是"SSL Proxying not enabled for this host: enable in Proxy Settings, SSL locations" 这行代码另外,当通过终端输入 curl 命令时,我无法在 Charles 上触发任何操作。我想这很正常。

SO 上的每个解决方案都告诉我重新启动模拟器,我这样做了。我去了模拟器设置并启用了 HTTP 服务。我在墨盒上吹了气,然后翻到了 Google 的第 2 页。请。帮助。我。

编辑 2:解决方案

这是最终代码,感谢@faarwa

    func performPostUberRequest() {
        var components = NSURLComponents()
        components.scheme = "https"
        components.host = "sandbox-api.uber.com"
        components.path = "/v1/products"
        components.queryItems = [
            NSURLQueryItem(name: "server_token", value: "********"),
            NSURLQueryItem(name: "latitude", value: "21.3088621"),
            NSURLQueryItem(name: "longitude", value: "-157.8086632")
        ]
        let request: NSMutableURLRequest = NSMutableURLRequest(URL: components.URL!)
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
print("thanks faarwa")
 }

【问题讨论】:

  • 很高兴你能成功。干杯!

标签: ios swift curl uber-api


【解决方案1】:

通过 URL 而不是 HTTP 消息正文发送查询参数,因为它是一个 GET 请求。删除:

    do {
        try request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
    } catch {

    }

并将参数添加到NSURL(例如使用NSURLComponents)应该可以解决这个问题。

【讨论】:

    猜你喜欢
    • 2014-12-11
    • 2014-10-11
    • 2017-05-07
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 2018-11-04
    相关资源
    最近更新 更多