如何将CURL命令转换为Swift

Par*_*ith 5 curl swift

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

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

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


卷曲

获取curl -u主机名:密码-数据OID1.1 = http://192.168.1.100:8100/get

设置curl -u主机名:密码--data OID1.10.5 = 120 --data Submit =提交 http://192.168.1.100:8100/pdp


API GET

使用的URL是/ get。API GET请求是未指定OID值的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=&
Run Code Online (Sandbox Code Playgroud)

响应

HTTP/1.1 200 OK
  Cache-control: no-cache
  Server: Ubicom/1.1
  Content-Length: 166
Run Code Online (Sandbox Code Playgroud)

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


API设置

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

请求

 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
Run Code Online (Sandbox Code Playgroud)

响应

HTTP/1.1 200 OK
  Cache-control: no-cache
  Server: Ubicom/1.1
  Content-Length: 308
Run Code Online (Sandbox Code Playgroud)

的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
Run Code Online (Sandbox Code Playgroud)

pbo*_*dsk 4

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

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

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

这些 API 采用的 URL 可能是/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 :-)
}
Run Code Online (Sandbox Code Playgroud)

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