Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用SOAP操作和HttpClient将ONVIF请求发送到具有授权的web摄像机_C#_Web Services_Http_Soap_Onvif - Fatal编程技术网

C# 如何使用SOAP操作和HttpClient将ONVIF请求发送到具有授权的web摄像机

C# 如何使用SOAP操作和HttpClient将ONVIF请求发送到具有授权的web摄像机,c#,web-services,http,soap,onvif,C#,Web Services,Http,Soap,Onvif,我正在从事一个在本地网络中发现和控制网络摄像机的项目。我是C++程序员,所以我在.NET中不好,但是这个项目我们在C语言上写的,我有一些问题。我正在使用DiscoveryClient查找本地网络中的所有设备。接下来,我获取摄像头地址,创建HttpClient并尝试发送SOAP操作。ONVIF规范:。某些操作(例如GetServiceCapabilities)返回响应,但大多数操作返回此错误: <env:Body><env:Fault><env:Code><

我正在从事一个在本地网络中发现和控制网络摄像机的项目。我是C++程序员,所以我在.NET中不好,但是这个项目我们在C语言上写的,我有一些问题。我正在使用DiscoveryClient查找本地网络中的所有设备。接下来,我获取摄像头地址,创建HttpClient并尝试发送SOAP操作。ONVIF规范:。某些操作(例如GetServiceCapabilities)返回响应,但大多数操作返回此错误:

<env:Body><env:Fault><env:Code><env:Value>env:Sender</env:Value>
<env:Subcode><env:Value>ter:NotAuthorized</env:Value>
</env:Subcode>
</env:Code>
<env:Reason><env:Text xml:lang="en">The action requested requires authorization and the sender is not authorized</env:Text>
</env:Reason>
</env:Fault>
</env:Body>
CreateSoapRequest()创建并返回的是我的SOAP请求:

公共字符串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=新的SHA1CryptoServiceProvider();
var passwordDigest=sha.ComputeHash(Encoding.UTF8.GetBytes(nonce64+date64+password64));
password64=Convert.tobase64字符串(passwordDigest);
这个.requestBodyString=
""
+ ""
+ ""
+ ""
+“”+this.login+“”
+“”+密码64+“”
+“”+nonce64+“”
+“”+this.dateCreated+“”
+ ""
+ ""
+ ""
+ ""
+ ""
+这是actionParameters
+ ""
+ "" +
“\r\n\r\n”;
返回此.requestBodyString;
}

谢谢你的帮助

我最近做了很多关于Onvif的工作,发现设置安全凭据相当麻烦。 首先,我想说的是,确保您的日期格式如下所示:

 var now = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddThh:mm:ss.fffZ");
与我的唯一不同之处(效果很好)是在标题中有一行额外的内容:

string xml = string.Format(@"<?xml version='1.0' encoding='UTF-8'?>"+
                                   "<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope'>" +
                                   "<s:Header>" +
                                   "<Security s:mustUnderstand='1' xmlns='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>" +
                                   "<UsernameToken>" +
                                   "<Username>{0}</Username>" +
                                   "<Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest'>" +
                                   "{1}" +
                                   "</Password>" +
                                   "<Nonce EncodingType='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'>" +
                                   "{2}" +
                                   "</Nonce>" +
                                   "<Created xmlns='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>" +
                                   "{3}" +
                                   "</Created>" +
                                   "</UsernameToken>" +
                                   "</Security>" +
                                   "</s:Header>", user, credentials[0], b64Nonce, credentials[2]);
string xml=string.Format(@“”+
"" +
"" +
"" +
"" +
"{0}" +
"" +
"{1}" +
"" +
"" +
"{2}" +
"" +
"" +
"{3}" +
"" +
"" +
"" +
“”,用户,凭据[0],b64Nonce,凭据[2]);

希望这有帮助

我已经处理这个问题很久了,下面是我用来获取所有身份验证信息的函数:

    static void GetPasswordDigest()
    {
        //Get nonce
        Random rnd = new Random();
        Byte[] nonce_b = new Byte[16];
        rnd.NextBytes(nonce_b);
        nonce64 = Convert.ToBase64String(nonce_b);
        Console.WriteLine("Nonce: " + nonce64);

        //Get timestamp
        DateTime created = DateTime.Now;
        creationtime = created.ToString("yyyy-MM-ddTHH:mm:ssZ");
        Byte[] creationtime_b = Encoding.ASCII.GetBytes(creationtime);
        Console.WriteLine("Timestamp: " + creationtime);

        //Convert the plain password to bytes
        Byte[] password_b = Encoding.ASCII.GetBytes(ONVIFPassword);

        //Concatenate nonce_b + creationtime_b + password_b
        Byte[] concatenation_b = new byte[nonce_b.Length + creationtime_b.Length + password_b.Length];
        System.Buffer.BlockCopy(nonce_b, 0, concatenation_b, 0, nonce_b.Length);
        System.Buffer.BlockCopy(creationtime_b, 0, concatenation_b, nonce_b.Length, creationtime_b.Length);
        System.Buffer.BlockCopy(password_b, 0, concatenation_b, nonce_b.Length + creationtime_b.Length, password_b.Length);

        //Apply SHA1 on the concatenation
        SHA1 sha = new SHA1CryptoServiceProvider();
        Byte[] pdresult = sha.ComputeHash(concatenation_b);
        passworddigest = Convert.ToBase64String(pdresult);
        Console.WriteLine("Password digest: " + passworddigest);

    } 
我希望这一切都会好起来

string xml = string.Format(@"<?xml version='1.0' encoding='UTF-8'?>"+
                                   "<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope'>" +
                                   "<s:Header>" +
                                   "<Security s:mustUnderstand='1' xmlns='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>" +
                                   "<UsernameToken>" +
                                   "<Username>{0}</Username>" +
                                   "<Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest'>" +
                                   "{1}" +
                                   "</Password>" +
                                   "<Nonce EncodingType='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'>" +
                                   "{2}" +
                                   "</Nonce>" +
                                   "<Created xmlns='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>" +
                                   "{3}" +
                                   "</Created>" +
                                   "</UsernameToken>" +
                                   "</Security>" +
                                   "</s:Header>", user, credentials[0], b64Nonce, credentials[2]);
    static void GetPasswordDigest()
    {
        //Get nonce
        Random rnd = new Random();
        Byte[] nonce_b = new Byte[16];
        rnd.NextBytes(nonce_b);
        nonce64 = Convert.ToBase64String(nonce_b);
        Console.WriteLine("Nonce: " + nonce64);

        //Get timestamp
        DateTime created = DateTime.Now;
        creationtime = created.ToString("yyyy-MM-ddTHH:mm:ssZ");
        Byte[] creationtime_b = Encoding.ASCII.GetBytes(creationtime);
        Console.WriteLine("Timestamp: " + creationtime);

        //Convert the plain password to bytes
        Byte[] password_b = Encoding.ASCII.GetBytes(ONVIFPassword);

        //Concatenate nonce_b + creationtime_b + password_b
        Byte[] concatenation_b = new byte[nonce_b.Length + creationtime_b.Length + password_b.Length];
        System.Buffer.BlockCopy(nonce_b, 0, concatenation_b, 0, nonce_b.Length);
        System.Buffer.BlockCopy(creationtime_b, 0, concatenation_b, nonce_b.Length, creationtime_b.Length);
        System.Buffer.BlockCopy(password_b, 0, concatenation_b, nonce_b.Length + creationtime_b.Length, password_b.Length);

        //Apply SHA1 on the concatenation
        SHA1 sha = new SHA1CryptoServiceProvider();
        Byte[] pdresult = sha.ComputeHash(concatenation_b);
        passworddigest = Convert.ToBase64String(pdresult);
        Console.WriteLine("Password digest: " + passworddigest);

    }