ios 带参数的 Swift GET 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27723912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:11:33  来源:igfitidea点击:

Swift GET request with parameters

iosswiftgetnsurlrequest

提问by MrSSS16

I'm very new to swift, so I will probably have a lot of faults in my code but what I'm trying to achieve is send a GETrequest to a localhost server with paramters. More so I'm trying to achieve it given my function take two parameters baseURL:string,params:NSDictionary. I am not sure how to combine those two into the actual URLRequest ? Here is what I have tried so far

我对 swift 很陌生,所以我的代码中可能会有很多错误,但我想要实现的是向GET带有参数的本地主机服务器发送请求。更重要的是,鉴于我的函数采用两个参数,我正试图实现它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)

    }

}

回答by Rob

When building a GETrequest, there is no body to the request, but rather everything goes on the URL. To build a URL (and properly percent escaping it), you can also use URLComponents.

构建GET请求时,请求没有正文,而是所有内容都在 URL 上。要构建 URL(并正确地对其进行百分比转义),您还可以使用URLComponents.

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

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

The only trick is that most web services need +character percent escaped (because they'll interpret that as a space character as dictated by the application/x-www-form-urlencodedspecification). But URLComponentswill not percent escape it. Apple contends that +is a valid character in a query and therefore shouldn't be escaped. Technically, they are correct, that it is allowed in a query of a URI, but it has a special meaning in application/x-www-form-urlencodedrequests and really should not be passed unescaped.

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

Apple acknowledges that we have to percent escaping the +characters, but advises that we do it manually:

Apple 承认我们必须对+字符进行百分比转义,但建议我们手动进行:

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")

This is an inelegant work-around, but it works, and is what Apple advises if your queries may include a +character and you have a server that interprets them as spaces.

这是一个不雅的解决方法,但它有效,如果您的查询可能包含一个+字符并且您有一个将它们解释为空格的服务器,Apple 会建议您这样做。

So, combining that with your sendRequestroutine, you end up with something like:

因此,将其与您的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()
}

And you'd call it like:

你会这样称呼它:

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

    // use `responseObject` here
}

Personally, I'd use JSONDecodernowadays and return a custom structrather than a dictionary, but that's not really relevant here. Hopefully this illustrates the basic idea of how to percent encode the parameters into the URL of a GET request.

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



See previous revision of this answerfor Swift 2 and manual percent escaping renditions.

请参阅此答案的先前修订版以了解 Swift 2 和手动转义百分比。

回答by Ben-Hur Batista

Use NSURLComponents to build your NSURL like this

使用 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

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

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

回答by anoop4real

I am using this, try it in playground. Define the base urls as Struct in Constants

我正在使用这个,在操场上试试。将基本网址定义为常量中的结构

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

回答by Danut Pralea

Swift 3:

斯威夫特 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 {
                 
var originalFilename = ""
if let url = info[UIImagePickerControllerReferenceURL] as? URL, let imageIdentifier = url.getQueryItemValueForKey(key: "id") {
    originalFilename = imageIdentifier + ".png"
    print("file name : \(originalFilename)")
}
.name.lowercased() == key.lowercased() }.first?.value } }

I used it to get the image name for UIImagePickerControllerin func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]):

我用它来获取UIImagePickerControllerin的图像名称func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

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

回答by Reza Shirazian

You can extend your Dictionaryto only provide stringFromHttpParameterif both key and value conform to CustomStringConvertablelike this

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

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)!
    }

}

this is much cleaner and prevents accidental calls to stringFromHttpParameterson dictionaries that have no business calling that method

这更干净,并防止意外调用stringFromHttpParameters没有业务调用该方法的字典

回答by etayluz

This extension that @Rob suggested works for Swift 3.0.1

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

I wasn't able to compile the version he included in his post with Xcode 8.1 (8B62)

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

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 Fabio Guerra

I use:

我用:

##代码##