在 .NET 中的 HttpWebResponse 中保留 XML 格式

发布于 2024-12-12 03:43:33 字数 998 浏览 0 评论 0原文

我有一个 .NET 项目,它通过 HTTP POST 以 SOAP 对象的形式将数据发布到外部 Web 服务器。这是使用 HttpWebRequest 对象完成的。我从 Web 服务器获得响应,并使用 HttpWebResponse 对象捕获该响应。该响应对象也是由 SOAP 信封包围的 XML。

问题是,当我获取响应并使用 ToString 将其输出到屏幕时,它显然会破坏所有标签并将其全部组合成一个字符串。

如何在不删除所有 XML 格式/标签的情况下输出从 Web 服务器返回的 XML?

这是我正在使用的代码:

HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(uri);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
request.Headers.Add("SOAPAction", "Some Headers");

byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();

requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
sResponse = new StreamReader(response.GetResponseStream()).ReadToEnd().ToString();

I have a .NET project which posts data to foreign web server in the form of a SOAP object via HTTP POST. This is done using a HttpWebRequest object. I get a response from the web server, which I am capturing with an HttpWebResponse object. This response object is also XML surrounded by a SOAP envelope.

The problem is, when I take the response and output it to the screen with ToString it apparently nukes all of the tags and just combines it all into a single string.

How can I output the returned XML from the web server without removing all the XML formatting/tags?

Here is the code I am using:

HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(uri);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
request.Headers.Add("SOAPAction", "Some Headers");

byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();

requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
sResponse = new StreamReader(response.GetResponseStream()).ReadToEnd().ToString();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2024-12-19 03:43:34

您可以将 xml 格式化为漂亮的打印形式,如下所示:

string xml = "<?xml version='1.0' encoding='UTF-8'?><foo><bar></bar></foo>";
XmlDocument document = new XmlDocument();
document.LoadXml(xml);

MemoryStream memStream = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(memStream, settings))
{
    document.Save(writer);
}
memStream.Flush();

string formattedXml = Encoding.UTF8.GetString(memStream.ToArray());

// strip UTF-8 BOM if required. Good for display purposes, can leave as such
// for normal processing.
string preAmble = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (formattedXml.StartsWith(preAmble)) 
{
    formattedXml = formattedXml.Remove(0, preAmble.Length);
    Console.WriteLine(formattedXml);
}

输出

<?xml version="1.0" encoding="utf-8"?>
<foo>
  <bar></bar>
</foo>

希望这会有所帮助。

干杯,
阿纳什

You could format an xml to pretty-printed form as follows:

string xml = "<?xml version='1.0' encoding='UTF-8'?><foo><bar></bar></foo>";
XmlDocument document = new XmlDocument();
document.LoadXml(xml);

MemoryStream memStream = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(memStream, settings))
{
    document.Save(writer);
}
memStream.Flush();

string formattedXml = Encoding.UTF8.GetString(memStream.ToArray());

// strip UTF-8 BOM if required. Good for display purposes, can leave as such
// for normal processing.
string preAmble = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (formattedXml.StartsWith(preAmble)) 
{
    formattedXml = formattedXml.Remove(0, preAmble.Length);
    Console.WriteLine(formattedXml);
}

Outputs

<?xml version="1.0" encoding="utf-8"?>
<foo>
  <bar></bar>
</foo>

Hope this helps.

Cheers,
Anash

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文