我正在使用HttpWebRequest发送Xml作为流但我收到以下响应:“prolog中不允许内容”
我应该使用编码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);
}
答案 0 :(得分:0)
Encoding
班级有 - BigEndianUnicode
获取使用大端字节顺序的UTF-16格式的编码。
因此,您可以更改代码以使用它(而不是ASCII
string finalXML = @"<?xml version=""1.0"" encoding=""UTF-16BE""?>";
byte[] bytes = Encoding.BigEndianUnicode.GetBytes(finalXML);
如果您真的对迷人的Unicode和编码世界感兴趣,可以查看this