How to generate GetSystemDateAndTime xml

点点圈 提交于 2019-11-28 14:47:13

This error is saying that there is trouble when trying to read a message, so i tough it was probably due to some sort of encoding ...

AND I WAS RIGHT!!

All I had to do was changing a parameter in the TextMessageEncodingBindingElement's creation.

MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressing10)

All you need to do is make sure that you have good encoding and AuthenticationScheme...

Here's my final code to get an onvif camera's (here cohuHD camera) system and date and time settings:

public bool Initialise(string cameraAddress, string userName, string password)
    {
        bool result = false;

        try
        {
            var messageElement = new TextMessageEncodingBindingElement()
            {
                MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressing10)
            };

            HttpTransportBindingElement httpBinding = new HttpTransportBindingElement()
            {
                AuthenticationScheme = AuthenticationSchemes.Digest
            };

            CustomBinding bind = new CustomBinding(messageElement, httpBinding);

            System.Net.ServicePointManager.Expect100Continue = false;

            DeviceClient deviceClient = new DeviceClient(bind, new EndpointAddress($"http://{cameraAddress}/onvif/device_service"));

            var temps = deviceClient.GetSystemDateAndTime();
        }
        catch (Exception ex)
        {
            ErrorMessage = ex.Message;
        }
        return result;
    }

Bonus:

If you want to execute a function that needs credentials, you can add those to your deviceClient like so:

//DIGEST (httpBinding)
deviceClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
deviceClient.ClientCredentials.HttpDigest.ClientCredential.UserName = userName;
deviceClient.ClientCredentials.HttpDigest.ClientCredential.Password = password;

Watch out also for the EndpointAddress' URL... I think some cameras use Device_service and other device_service .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!