Why when i insert encoding=“UTF-16BE” in xml the h

2019-08-28 06:35发布

The following method generate a XmlDocument from a string and then call another method to generate a hash of the XmlDocument created.

private void geraXML()
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.PreserveWhitespace = true;
        string xml = @"<?xml version=""1.0""?>...";

        xmlDoc.LoadXml(xml);
        string caminho = path/+"xmldoc.xml";            
        string nomeArquivo = "xmldoc.xml";
        xmlDoc.Save(caminho);                     

        //call method to generate hash
        geraHASH(caminho, nomeArquivo);            
    }

This another method convert the same string of first method to stream and send to a webService

private void enviaACCS001(string protocolo, string caminho)
    {
        string base64 = Convert.ToBase64String(Encoding.Default.GetBytes("user:password"));
        string authorization = String.Concat("Basic ", base64);

        String finalResult;
        HttpWebRequest hwrRequest = (HttpWebRequest)HttpWebRequest.Create("addres/" + protocolo);

        hwrRequest.UseDefaultCredentials = true;
        hwrRequest.Headers.Add("Authorization", authorization);
        hwrRequest.Method = "PUT";

 string finalXML = @"<?xml version=""1.0""?>...";
 byte[] bytes = Encoding.Default.GetBytes(finalXML);
hwrRequest.ContentLength = bytes.Length;

        using (Stream putStream = hwrRequest.GetRequestStream())
        {
            putStream.Write(bytes, 0, bytes.Length);
        }

        using (HttpWebResponse response = (HttpWebResponse)hwrRequest.GetResponse())
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            finalResult = reader.ReadToEnd();
            visualiza.Text = visualiza.Text + "\n " + finalResult;
        }

The WebService generate the hash of stream and compare with the first hash generated. So far so good, the compare return true, but the WebService need the encoding="UTF-16BE" and when insert this information into the string the hash not match. What i'm doing wrong?

First method with encoding="UTF-16BE"

private void geraXML()
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.PreserveWhitespace = true;
    string xml = @"<?xml version=""1.0"" encoding=""UTF-16BE""?>...";

    xmlDoc.LoadXml(xml);
    string caminho = path/+"xmldoc.xml";            
    string nomeArquivo = "xmldoc.xml";
    xmlDoc.Save(caminho);                     

    //call method to generate hash
    geraHASH(caminho, nomeArquivo);            
}

Second method with encoding="utf-16bE"

private void enviaACCS001(string protocolo, string caminho)
    {
        string base64 = Convert.ToBase64String(Encoding.Default.GetBytes("user:password"));
        string authorization = String.Concat("Basic ", base64);

        String finalResult;
        HttpWebRequest hwrRequest = (HttpWebRequest)HttpWebRequest.Create("addres/" + protocolo);

        hwrRequest.UseDefaultCredentials = true;
        hwrRequest.Headers.Add("Authorization", authorization);
        hwrRequest.Method = "PUT";

 string finalXML = @"<?xml version=""1.0"" encoding=""UTF-16BE""?>...";
 byte[] bytes = Encoding.BigEndianUnicode.GetBytes(finalXML);
hwrRequest.ContentLength = bytes.Length;

        using (Stream putStream = hwrRequest.GetRequestStream())
        {
            putStream.Write(bytes, 0, bytes.Length);
        }

        using (HttpWebResponse response = (HttpWebResponse)hwrRequest.GetResponse())
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            finalResult = reader.ReadToEnd();
            visualiza.Text = visualiza.Text + "\n " + finalResult;
        }

0条回答
登录 后发表回答