我正在学习编写我的第一个 IOS 应用程序,它将从 Proliphix IP 网络恒温器查询一些基本的 OID。 Proliphix 支持多种方法,例如 Curl; PHP 和 API 获取和设置。在这些方法中,Swift 中最简单的方法是什么?
谁能告诉我如何为 Swift 转换以下方法之一?
以下是来自 Proliphix API 的示例,可以在谷歌搜索中找到。
curl
得到 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=提交 http://192.168.1.100:8100/pdp
API 获取
使用的 URL 是/get。 API GET 请求是未指定值的 OID 列表。格式正确的请求应提供 Content-Length header 。 .该条目是编码的基本身份验证字(请参阅 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@prolipix.com&OID1.2=软件开发 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
回应
HTTP/1.1 200 OK
Cache-control: no-cache
Server: Ubicom/1.1
Content-Length: 308
PHP
PHP 是一种特定于网络服务器的脚本语言,类似于 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
最佳答案
我会说您应该使用 Proliphix 提供的 API。
如您所见,他们提供了一个示例,您已经设法弄清楚如何通过 cURL 提供正确的参数,所以现在您“只需”将其转换为 Swift。
为此你需要一个 HTTP 网络 API,你可以使用 NSURLSession Apple 提供的 API,或者可能是 Alamofire , 仅举一对。
这些 API 采用的 URL 在您的情况下为 /get
或 /pdp
。然后你需要告诉他们这是 GET 还是 POST 请求。如果 API 需要任何数据(如您的情况下的 OID 参数),您也需要提供该数据,然后您需要设置最终 header 。
然后您发送您的请求并等待答复,然后您对答复作出 react 。
这是一个关于如何使用 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 教程,现在您知道要搜索什么了。
关于swift - 如何将 CURL 命令转换为 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30723703/