在 .NET 中的 HttpWebResponse 中保留 XML 格式
我有一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 xml 格式化为漂亮的打印形式,如下所示:
输出
希望这会有所帮助。
干杯,
阿纳什
You could format an xml to pretty-printed form as follows:
Outputs
Hope this helps.
Cheers,
Anash