如何将CURL命令转换为Swift

时间:2015-06-09 05:33:07

标签: swift curl

我正在学习编写我的第一个IOS应用程序,它将从Proliphix IP网络温控器查询一些基本的OID。 Proliphix支持几种方法,如Curl; PHP和API GET&组。在这些方法中,Swift中最简单的是什么?

有人可以告诉我如何为Swift转换以下方法之一吗?

以下是可以在Google搜索中找到的Proliphix API的示例。

卷曲

获取 curl -u hostname:password --data OID1.1 = http://192.168.1.100:8100/get

设置       curl -u hostname:password --data OID1.10.5 = 120 --data submit = Submit       http://192.168.1.100:8100/pdp

API GET

使用的URL是/ get。 API GET请求是未指定其值的OID列表。格式正确的请求应提供Content-Length标头。 。该条目是编码的基本认证字(参见RFC 2617 -HTTP认证:基本和摘要访问认证)。

请求

POST /get HTTP/1.1
  Authorization: Basic <credentials>
  Content-Type: application/x-www-form-urlencoded
  User-Agent: Jakarta Commons-HttpClient/2.0.2
  Host: 192.168.111.114:8214
  Content-Length: 92
  OID1.10.9=&OID1.2=&OID1.1=&OID1.4=&OID1.8=&OID2.7.1=&

响应

HTTP/1.1 200 OK
  Cache-control: no-cache
  Server: Ubicom/1.1
  Content-Length: 166

OID1.10.9=example@proliphix.com& OID1.2 = SW Dev 114&amp; OID1.1 = therm_rev_2 0.1.40&amp; OID1.4 = 192.168.111.114&amp; OID1.8 = 00:11:49:00 :00:58&安培; OID2.7.1 = NT100

API SET

使用的URL是/ pdp。 API SET类似于请求消息的API GET,除了在等号处提供所需的值。响应的格式不同。该条目是编码的基本认证字(参见RFC 2617 -HTTP认证:基本和摘要访问认证)。请求中的最后一项必须是“submit = Submit”。在&#34; submit = Submit&#34;。

之后不要包含'&amp;'

请求

 POST /pdp HTTP/1.1
  Authorization: Basic <credentials>
  Content-Type: application/x-www-form-urlencoded
  User-Agent: Jakarta Commons-HttpClient/2.0.2
  Host: 192.168.111.114:8214
  Content-Length: 193

响应

HTTP/1.1 200 OK
  Cache-control: no-cache
  Server: Ubicom/1.1
  Content-Length: 308

PHP

PHP是一种特定于Web服务器的脚本语言,类似于mod_perl。它很好地集成到Apache中,并提供许多特定于Web的库作为基本系统的一部分。

获取

$oids = array('OID1.4'=>'', // commonIpAddr
              'OID1.10.5'=>'',
              ‘submit’=>’Submit’); // commonCallhomeInterval
$url = “http://192.168.1.100:8100/get”;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$myHeader = array("Content-Type: application/x-www-form-urlencoded" ); curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($oids)); $response = curl_exec($ch);
curl_close($ch);
$oids = array();
parse_str($response, $oids); // converts '.' to underscore
$localip = $oids['OID1_4'];
$interval = $oids['OID1_10_5']; // in minutes

1 个答案:

答案 0 :(得分:4)

我想说你应该使用Proliphix提供的API。

正如您所看到的,它们提供了一个示例,您已经设法弄清楚如何通过cURL提供正确的参数,所以现在您“只需”将其转换为Swift。

为此,您需要一个HTTP网络API,您可以使用Apple提供的NSURLSession API,也可以使用Alamofire,只需提及一对。

这些API会在您的案例中使用/get/pdp的网址。然后你需要告诉他们这是一个GET或POST请求。如果API需要任何数据(例如您的OID参数),您还需要提供该数据,然后您需要设置最终标头。

然后你发送你的请求并等待答案,然后你做出反应。

以下是有关如何使用NSURLSession执行此操作的示例:

if let url = NSURL(string: "http://httpbin.org/post"){
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST" //Or GET if that's what you need
    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")  //This is where you add your HTTP headers like Content-Type, Accept and so on
    let params = ["OID1.2" : "SW+Dev+114", "OID1.4" : "192.168.111.114"] as Dictionary<String, String> //this is where you add your parameters

    let httpData = NSKeyedArchiver.archivedDataWithRootObject(params) //you need to convert you parameters to NSData or to JSON data if the service accepts this, you might want to search for a solution on how to do this...hopefully this will get you in the right direction :-)
    request.HTTPBody = httpData
    let session = NSURLSession.sharedSession()
    session.dataTaskWithRequest(request, completionHandler: { (returnData, response, error) -> Void in
        var strData = NSString(data: returnData, encoding: NSUTF8StringEncoding)
        println("\(strData)")
    }).resume() //Remember this one or nothing will happen :-)
}

希望这能让你朝着正确的方向前进。既然您知道要搜索什么,也可以在Google搜索NSURLSession或Alamofire教程。

相关问题