如何在Swift中发出HTTP请求? [英] How to make an HTTP request in Swift?

查看:263
本文介绍了如何在Swift中发出HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iBooks中阅读了Apple编程语言Swift,但无法弄清楚如何在Swift中发出一个http请求(类似于CURL)。我是否需要导入Obj-C类或者我只需要导入默认库?或者是否无法根据本机swift代码发出HTTP请求?

I read The Programming Language Swift by Apple in iBooks, but cannot figure out how to make an http request (something like CURL) in Swift. Do I need to import Obj-C classes or do I just need to import default libraries? Or is it not possible to make an HTTP request based on native swift code?

推荐答案

另一种选择是 Alamofire 提供可链接请求/响应方法的库。

Another option is the Alamofire lib that offers Chainable Request / Response methods.

https://github.com/Alamofire/Alamofire

提出请求

import Alamofire

Alamofire.request(.GET, "http://httpbin.org/get")

回应处理

Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
         .response { request, response, data, error in
              print(request)
              print(response)
              print(error)
          }

这篇关于如何在Swift中发出HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆