背景
之前的文章已经介绍了怎么利用ONVIF搜索发现设备,以及如何进行鉴权和认证。接下来的工作就是获取设备的信息,包括设备类型、设备序列号等。
目录
文章发表的顺序可能有点乱,麻烦根据《基于ONVIF协议的摄像机监控管理平台(1)概述》里面的文章目录进行阅读,抱歉了
一、获取设备信息的接口
先贴一下官方文档的接口说明吧。
GetDeviceInformation
Description:
This operation gets basic device information from the device.
SOAP action:
http://www.onvif.org/ver10/device/wsdl/GetDeviceInformation
Input:
[GetDeviceInformation]
Output:
[GetDeviceInformationResponse]
Manufacturer [string]
The manufactor of the device.
Model [string]
The device model.
FirmwareVersion [string]
The firmware version in the device.
SerialNumber [string]
The serial number of the device.
HardwareId [string]
The hardware ID of the device.
二、Output的参数说明
- Manufacturer [string]
- 设备制造商;
- Model [string]
- 设备型号
- FirmwareVersion [string]
- 设备的固件系统版本;
- SerialNumber [string]
- 设备序列号;
- HardwareId [string]
- 硬件的ID;
三、请求信息
部分(非标准或者山寨)设备,调用这个接口是不需要鉴权的。那么基本的请求信息如下所示。
<?xml version="1.0" encoding="utf-8" ?>
<s:Envelope
xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
</s:Header>
<s:Body
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetDeviceInformation
xmlns="http://www.onvif.org/ver10/device/wsdl"/>
</s:Body>
</s:Envelope>
四、添加鉴权信息
- 但是,一般情况下,调用这个接口都是需要添加鉴权信息的。
- 鉴权信息的格式,以及鉴权的流程和原理可以参考我上几篇文章。
- 我这里采用的是 Web Service 层的 username token鉴权。
- 鉴权的信息和内容,写在XML的 header里面。
- 具体的xml内容可以参考下面。
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>admin</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">VuQZMCvg43M0m5XmI7xypINx2m4=</wsse:Password>
<wsse:Nonce>gpQxcoOf2Jm5c2gyhXWHAw==</wsse:Nonce>
<wsu:Created>2017-12-30T04:32:49Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetDeviceInformation xmlns="http://www.onvif.org/ver10/device/wsdl"></GetDeviceInformation>
</s:Body>
</s:Envelope>
五、参考代码
先说说简单思路吧。
- 用xml库生成一个xml对象,然后按照上面的结构添加相应的节点信息。
- 或者直接复制上面的字符串,用它生成一个xml对象,然后修改信息。
- 用上几篇文章提到的 username token digest 算法生成相应的信息,并且赋给xml 对象。
- 利用这个xml发起 http post请求。
- 等待服务端的响应。
- 解析服务端的应答信息。
代码如下:
private async void HttpGetDeviceInfo()
{
//ONVIF服务所在的地址
var url = "http://192.168.110.163/onvif/device_service";
//创建一个http请求
using (var client = new HttpClient())
{
//清楚http请求的所有默认Header,在Header中添加SOAPAction
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("SOAPAction", "https://www.onvif.org/ver10/device/wsdl/GetDeviceInformation");
//这段xml字符串是复制过来的,用它生成一个xml结构,然后选择性的修改一部分内容
String xmlStr = "<?xml version=\"1.0\" encoding=\"utf - 8\"?><s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Header><wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><wsse:UsernameToken><wsse:Username>admin</wsse:Username><wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">59zlfHOLcoptDNzx2MVvsIiqcow=</wsse:Password><wsse:Nonce>hlCkYZgzndYSXPcrr+RRXg==</wsse:Nonce><wsu:Created>2017-12-28T09:57:22Z</wsu:Created></wsse:UsernameToken></wsse:Security></s:Header><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><GetDeviceInformation xmlns=\"http://www.onvif.org/ver10/device/wsdl\"/></s:Body></s:Envelope>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr);
XmlNode username = doc.GetElementsByTagName("wsse:Username").Item(0);
XmlNode digest = doc.GetElementsByTagName("wsse:Password").Item(0);
XmlNode nonce = doc.GetElementsByTagName("wsse:Nonce").Item(0);
XmlNode created = doc.GetElementsByTagName("wsu:Created").Item(0);
Console.WriteLine(username.InnerText.ToString());
Console.WriteLine(digest.InnerText.ToString());
Console.WriteLine(nonce.InnerText.ToString());
Console.WriteLine(created.InnerText.ToString());
//生成请求和相应的参数
username.InnerText = "admin";
nonce.InnerText = GetNonce();
created.InnerText = GetCreated();
digest.InnerText = GetPasswordDigest(nonce.InnerText, created.InnerText, "123456");
Console.WriteLine(doc.OuterXml);
//设置一下 http post的格式
var content = new StringContent(doc.OuterXml, Encoding.UTF8, "application/xml");
//发起请求
var response = await client.PostAsync(url, content);
//查看响应信息
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString.ToString());
}
}
如果没问题的话,收到设备的返回结果大致如下。
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:xmime4="http://www.w3.org/2004/11/xmlmime" xmlns:wsa5="http://www.w3.org/2005/08/addressing" xmlns:wsrf-bf="http://docs.oasis-open.org/wsrf/bf-2" xmlns:wstop="http://docs.oasis-open.org/wsn/t-1" xmlns:wsrf-r="http://docs.oasis-open.org/wsrf/r-2" xmlns:tes-e="http://www.onvif.org/ver10/events/wsdl/EventBinding" xmlns:tev="http://www.onvif.org/ver10/events/wsdl" xmlns:tes-nc="http://www.onvif.org/ver10/events/wsdl/NotificationConsumerBinding" xmlns:tes-np="http://www.onvif.org/ver10/events/wsdl/NotificationProducerBinding" xmlns:tes-sm="http://www.onvif.org/ver10/events/wsdl/SubscriptionManagerBinding" xmlns:tns1="http://www.onvif.org/ver10/topics" xmlns:xmime="http://www.w3.org/2004/06/xmlmime" xmlns:tt="http://www.onvif.org/ver10/schema" xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl" xmlns:tmd="http://www.onvif.org/ver10/deviceIO/wsdl" xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl" xmlns:trt="http://www.onvif.org/ver10/media/wsdl" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ter="http://www.onvif.org/ver10/error" xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl" xmlns:tan-ae="http://www.onvif.org/ver20/analytics/wsdl/AnalyticsEngineBinding" xmlns:tan-re="http://www.onvif.org/ver20/analytics/wsdl/RuleEngineBinding" xmlns:trc="http://www.onvif.org/ver10/recording/wsdl" xmlns:trp="http://www.onvif.org/ver10/replay/wsdl" xmlns:tse="http://www.onvif.org/ver10/search/wsdl" xmlns:tpl="http://www.onvif.org/ver10/plus/wsdl" xmlns:tplt="http://www.onvif.org/ver10/plus/schema">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<tds:GetDeviceInformationResponse>
<tds:Manufacturer>UNIVIEW</tds:Manufacturer>
<tds:Model>IPC244S-IR5-F36-DT</tds:Model>
<tds:FirmwareVersion>IPC_G6102-B5011P10D1604</tds:FirmwareVersion>
<tds:SerialNumber>210235C1X8A166000091</tds:SerialNumber>
<tds:HardwareId>IPC244S-IR5-F36-DT</tds:HardwareId>
</tds:GetDeviceInformationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
后记
有任何的疑问或者想法,或者对本文有质疑或者补充的话,欢迎在留言区评论,期待你的分享!
同时,也欢迎关注扫描屏幕下方关注本人微信公众号或者打赏一下这篇文章。
http://xzh.i3geek.com
0 条评论