作者:用户9o05le1po7 | 来源:互联网 | 2023-09-09 10:42
我正在研究在局域网中发现和控制网络摄像机的项目.我是C程序员,所以我不太擅长.NET,但是我们在C#上编写的这个项目遇到了一些问题.我正在使用DiscoveryClient查找本地
我正在研究在局域网中发现和控制网络摄像机的项目.我是C程序员,所以我不太擅长.NET,但是我们在C#上编写的这个项目遇到了一些问题.我正在使用DiscoveryClient查找本地网络中的所有设备.接下来,我获得摄像机地址,创建HttpClient并尝试发送SOAP操作. ONVIF规范:http://www.onvif.org/onvif/ver10/device/wsdl/devicemgmt.wsdl.某些操作(例如GetServiceCapabilities)返回响应,但大多数操作都会返回此错误:
env:Sender
ter:NotAuthorized
The action requested requires authorization and the sender is not authorized
我正在像官方ONVIF文档(第35-36页)中那样创建SOAP请求. http://www.onvif.org/Portals/0/documents/WhitePapers/ONVIF_WG-APG-Application_Programmer‘s_Guide.pdf. “ admin”和“ 12345”-是我们测试网络摄像头的登录名和密码.
这是我尝试在下面发送请求的代码:
HttpClient httpClient = new HttpClient();
var byteArray = Encoding.UTF8.GetBytes("admin:12345");
var request = requestStructure.CreateSoapRequest();
httpClient.DefaultRequestHeaders.Add("SOAPACTION", "\"" + requestStructure.actionNamespace + "#" + requestStructure.actionName + "\"");
httpClient.DefaultRequestHeaders.Add("Authorization", "Digest " + Convert.ToBase64String(byteArray));
var resp = await httpClient.PostAsync(requestedUri, new StringContent(request, UnicodeEncoding.UTF8));
var respString = await resp.Content.ReadAsStringAsync();
由CreateSoapRequest()创建并返回的是我的SOAP请求:
public string CreateSoapRequest()
{
var nonce64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.nonce.ToString()));
var date64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.dateCreated));
var password64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.password));
SHA1 sha = new SHA1CryptoServiceProvider();
var passwordDigest = sha.ComputeHash(Encoding.UTF8.GetBytes(nonce64 + date64 + password64));
password64 = Convert.ToBase64String(passwordDigest);
this.requestBodyString =
" + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
+ "soap:encodinghttp://schemas.xmlsoap.org/soap/encoding/\">"
+ ""
+ ""
+ ""
+ "" + this.login + ""
+ "" + password64 + ""
+ "" + nonce64 + ""
+ "" + this.dateCreated + ""
+ "
"
+ ""
+ ""
+ ""
+ " + "xmlns:u=\"" + this.actionNamespace + "\">"
+ this.actionParameters
+ ""
+ "" +
"\r\n\r\n";
return this.requestBodyString;
}
谢谢你的帮助!
解决方法:
我最近在做很多Onvif的工作,发现设置安全凭证非常麻烦.
首先,我要确保您的日期格式如下所示:
var now = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddThh:mm:ss.fffZ");
与我的唯一区别(工作正常)是我在标题中多了一行:
string xml = string.Format(@""+
"" +
"" +
"" +
"" +
"{0}" +
"" +
"{1}" +
"" +
"" +
"{2}" +
"" +
"" +
"{3}" +
"" +
"
" +
"" +
"", user, credentials[0], b64Nonce, credentials[2]);
希望这可以帮助!