带参数的Swift GET请求


81

我是新手,所以我的代码可能会出现很多错误,但是我要实现的目标是将GET请求发送到带有参数的localhost服务器。鉴于我的函数有两个参数,我试图做到更多baseURL:string,params:NSDictionary。我不确定如何将两者结合到实际的URLRequest中?到目前为止,这是我尝试过的

    func sendRequest(url:String,params:NSDictionary){
       let urls: NSURL! = NSURL(string:url)
       var request = NSMutableURLRequest(URL:urls)
       request.HTTPMethod = "GET"
       var data:NSData! =  NSKeyedArchiver.archivedDataWithRootObject(params)
       request.HTTPBody = data
       println(request)
       var session = NSURLSession.sharedSession()
       var task = session.dataTaskWithRequest(request, completionHandler:loadedData)
       task.resume()

    }

}

func loadedData(data:NSData!,response:NSURLResponse!,err:NSError!){
    if(err != nil){
        println(err?.description)
    }else{
        var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
        println(jsonResult)

    }

}

Answers:


156

建立GET请求时,请求的主体没有,但是所有内容都放在URL上。要构建网址(并正确地将其转义),您还可以使用URLComponents

var url = URLComponents(string: "https://www.google.com/search/")!

url.queryItems = [
    URLQueryItem(name: "q", value: "War & Peace")
]

唯一的窍门是,大多数Web服务都需要+转义字符百分比(因为它们会将其解释为application/x-www-form-urlencoded规范所规定的空格字符)。但是URLComponents不会百分百逃脱它。Apple认为这+是查询中的有效字符,因此不应转义。从技术上讲,它们是正确的,它可以在URI查询中使用,但在application/x-www-form-urlencoded请求中具有特殊含义,因此实际上不应将其转义。

苹果公司承认我们必须对+字符进行转义,但建议我们手动进行:

var url = URLComponents(string: "https://www.wolframalpha.com/input/")!

url.queryItems = [
    URLQueryItem(name: "i", value: "1+2")
]

url.percentEncodedQuery = url.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")

这是一个不错的解决方法,但是它可以工作,这是Apple建议的,如果您的查询中可能包含+字符,并且您拥有一台将其解释为空格的服务器。

因此,将其与您的sendRequest例行程序结合起来,最终得到的结果是:

func sendRequest(_ url: String, parameters: [String: String], completion: @escaping ([String: Any]?, Error?) -> Void) {
    var components = URLComponents(string: url)!
    components.queryItems = parameters.map { (key, value) in 
        URLQueryItem(name: key, value: value) 
    }
    components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
    let request = URLRequest(url: components.url!)

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data,                            // is there data
            let response = response as? HTTPURLResponse,  // is there HTTP response
            (200 ..< 300) ~= response.statusCode,         // is statusCode 2XX
            error == nil else {                           // was there no error, otherwise ...
                completion(nil, error)
                return
        }

        let responseObject = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
        completion(responseObject, nil)
    }
    task.resume()
}

你会这样称呼它:

sendRequest("someurl", parameters: ["foo": "bar"]) { responseObject, error in
    guard let responseObject = responseObject, error == nil else {
        print(error ?? "Unknown error")
        return
    }

    // use `responseObject` here
}

就我个人而言,JSONDecoder如今我会使用它并返回一个自定义struct而不是字典,但这在这里并不重要。希望这说明了如何将参数百分比编码到GET请求的URL中的基本思想。


有关Swift 2和手动百分比转义表示法,请参阅此答案的先前版本


感谢您的精彩回答。我只有一个问题,我extension string对值在做什么感到有些困惑?另外我HttpBody什么时候需要使用呢?
MrSSS16 2015年

字符串扩展名是按照RFC 3986的规定对值进行转义的百分比。某些字符在URL中具有特殊含义(例如,&将一个参数与下一个参数分开,因此,如果&出现在值中,则不能放任其转义)。至于HTTPBody,你应该不是在使用它GET的要求; 它用于POST但不使用GET
罗布(Rob)

这看起来很简单,而不是使用github.com/xyyc/SwiftSocket之类的东西有什么优势?抱歉,我是新手。
jigzat 2015年

这可能不是正确的比较,因为那是一个套接字库,这是HTTP。更像Alamofire之类的东西,但是(a)更灵活(可以处理JSON请求,x-www-form-urlencoded请求,多部分等);(b)具有更多功能(例如,处理身份验证挑战);(c)让您摆脱HTTP编程的麻烦。如果有的话,我在上面的回答只是为了提醒自己创建自己的风险NSMutableURLRequest,并指出它所带来的影响超出了OP的建议。
罗布(Rob)

这是一个很好的解决方案。谢谢@Rob。顺便说一句,提供这样的参数有什么区别NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: nil)吗?parameters是一系列字典[String: String]
Isuru 2015年

94

使用NSURLComponents这样构建您的NSURL

var urlComponents = NSURLComponents(string: "https://www.google.de/maps/")!

urlComponents.queryItems = [
  NSURLQueryItem(name: "q", value: String(51.500833)+","+String(-0.141944)),
  NSURLQueryItem(name: "z", value: String(6))
]
urlComponents.URL // returns https://www.google.de/maps/?q=51.500833,-0.141944&z=6

字体:https//www.ralfebert.de/snippets/ios/encoding-nsurl-get-parameters/


5
尽管Rob给出了令人印象深刻的答案,但您的答案更简单且有效。
1月ATAC 2013年

3
这应该是公认的答案。建议将NSURLComponents与查询项一起使用以构造URL。更安全,更不容易出错。
约翰·罗杰斯

4

我正在使用,请在操场上尝试。在常量中将基本URL定义为“结构”

struct Constants {

    struct APIDetails {
        static let APIScheme = "https"
        static let APIHost = "restcountries.eu"
        static let APIPath = "/rest/v1/alpha/"
    }
}

private func createURLFromParameters(parameters: [String:Any], pathparam: String?) -> URL {

    var components = URLComponents()
    components.scheme = Constants.APIDetails.APIScheme
    components.host   = Constants.APIDetails.APIHost
    components.path   = Constants.APIDetails.APIPath
    if let paramPath = pathparam {
        components.path = Constants.APIDetails.APIPath + "\(paramPath)"
    }
    if !parameters.isEmpty {
        components.queryItems = [URLQueryItem]()
        for (key, value) in parameters {
            let queryItem = URLQueryItem(name: key, value: "\(value)")
            components.queryItems!.append(queryItem)
        }
    }

    return components.url!
}

let url = createURLFromParameters(parameters: ["fullText" : "true"], pathparam: "IN")

//Result url= https://restcountries.eu/rest/v1/alpha/IN?fullText=true

1

迅捷3

extension URL {
    func getQueryItemValueForKey(key: String) -> String? {
        guard let components = NSURLComponents(url: self, resolvingAgainstBaseURL: false) else {
              return nil
        }

        guard let queryItems = components.queryItems else { return nil }
     return queryItems.filter {
                 $0.name.lowercased() == key.lowercased()
                 }.first?.value
    }
}

我用它来为获得图像名称UIImagePickerControllerfunc imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

var originalFilename = ""
if let url = info[UIImagePickerControllerReferenceURL] as? URL, let imageIdentifier = url.getQueryItemValueForKey(key: "id") {
    originalFilename = imageIdentifier + ".png"
    print("file name : \(originalFilename)")
}

您的回应未回答OP的问题
Ilias Karim

0

您可以将其扩展Dictionary为仅stringFromHttpParameter在键和值都符合CustomStringConvertable这样的条件下提供

extension Dictionary where Key : CustomStringConvertible, Value : CustomStringConvertible {
  func stringFromHttpParameters() -> String {
    var parametersString = ""
    for (key, value) in self {
      parametersString += key.description + "=" + value.description + "&"
    }
    return parametersString
  }
}

这样更清洁,可以防止意外调用stringFromHttpParameters没有业务的词典。


-1

@Rob建议的扩展名适用于Swift 3.0.1

我无法使用Xcode 8.1(8B62)编译他包含在他的帖子中的版本

extension Dictionary {

    /// Build string representation of HTTP parameter dictionary of keys and objects
    ///
    /// :returns: String representation in the form of key1=value1&key2=value2 where the keys and values are percent escaped

    func stringFromHttpParameters() -> String {

        var parametersString = ""
        for (key, value) in self {
            if let key = key as? String,
               let value = value as? String {
                parametersString = parametersString + key + "=" + value + "&"
            }
        }
        parametersString = parametersString.substring(to: parametersString.index(before: parametersString.endIndex))
        return parametersString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
    }

}

-2

我用:

let dictionary = ["method":"login_user",
                  "cel":mobile.text!
                  "password":password.text!] as  Dictionary<String,String>

for (key, value) in dictionary {
    data=data+"&"+key+"="+value
    }

request.HTTPBody = data.dataUsingEncoding(NSUTF8StringEncoding);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.