0
我使用的HttpWebRequest發送XML作爲數據流,但我會收到以下消息:「內容是不允許在序言」如何創建一個XML(編碼UTF-16BE)和HttpWebRequest的發送爲流
我應該使用編碼UTF-16BE,但我不知道如何。
我當前的代碼:
private void sendXml(string protocolo, string path)
{
string base64 = Convert.ToBase64String(Encoding.Default.GetBytes("user:password"));
string authorization = String.Concat("Basic ", base64);
String finalResult;
HttpWebRequest hwrRequest = (HttpWebRequest)HttpWebRequest.Create("https://address/"+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;
}
}
方法生成XMLDOCUMENT
private void geraXML()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = false;
byte[] xml = Encoding.BigEndianUnicode.GetBytes(@"<?xml version=""1.0"" encoding=""UTF-16BE""?>...");
MemoryStream ms = new MemoryStream(xml, true);
xmlDoc.Load(ms);
string caminho = path/+"xmldoc.xml";
string nomeArquivo = "xmldoc.xml";
xmlDoc.Save(caminho);
geraHASH(caminho, nomeArquivo);
}
我在發送finalXML之前,我有一個方法可以從字符串finalXML創建一個XmlDocument,並生成一個用於請求協議的哈希,並且比我用這個協議號發送finalXML。當Webservice接收到流時,它們會爲com生成一個新的散列與用於請求協議的哈希碼相比較。這就是問題所在。哈希碼是不同的。 – Periclowisk
在這篇文章中,我解釋更多的問題:https://stackoverflow.com/questions/45333005/why-when-i-insert-encoding-utf-16be-in-xml-tha-hash-is-different – Periclowisk