简体   繁体   English

获取400或500 HTTP XML响应的XML主体

[英]Get XML body of a 400 or 500 HTTP XML response

Long story short, I am sending an XML HTTP post request to an application server, and I am getting back a response, also in the form of XML HTTP. 简而言之,我正在向应用服务器发送XML HTTP post请求,并且我还以XML HTTP的形式返回响应。

I have a test site available to me which allows me to see what the server's actual response is, visually, in the form of XML, but I cannot access this XML from my C# code the way it is. 我有一个可用的测试站点,它允许我以XML的形式直观地查看服务器的实际响应,但我不能按照它的方式从我的C#代码中访问这个XML。

The XML coming back from the application server in my test case looks like this: 在我的测试用例中从应用程序服务器返回的XML如下所示:

<Error><Message>StringErrorMessage</Message></Error>

However, I have had no luck accessing this basic XML to retrieve the value of "StringErrorMessage" for the creation of a detailed error report. 但是,我没有幸运访问这个基本的XML来检索“StringErrorMessage”的值来创建详细的错误报告。

... More code above, all wrapped in a try{}...

_response = Serializer.DeserializeObject<T>(ObjectRequest.GetResponse().GetResponseStream());

        }
        catch (System.Net.WebException exceptionParameter)
        {
            var response = (HttpWebResponse)exceptionParameter.Response;
            string webExceptionStatus = exceptionParameter.Message;
            _exception = exceptionParameter;
            return false;
        }

I have consulted C# - Getting the response body from a 403 error 我咨询过C# - 从403错误中获取响应正文

and

Get response body on 400 HTTP response in Android? 在Android中获得400 HTTP响应的响应正文?

The first link's solution doesn't seem to give me access to the basic XML as part of any response object's properties. 第一个链接的解决方案似乎不允许我访问基本XML作为任何响应对象属性的一部分。 I am almost positive that there must be a byte[] in there somewhere (in the response, or in the exception object) that can be converted into a char[], which can be converted to a string, which can be converted to my XML body, but I have not been able to find it. 我几乎肯定在某处(在响应中,或在异常对象中)必须有一个byte []可以转换为char [],它可以转换为字符串,可以转换为我的字符串XML体,但我一直无法找到它。 The second link's solution is not exactly viable for me because I have to get the response body back in the form of XML, as it might not be an error, but an object that must be deserialized. 第二个链接的解决方案对我来说并不完全可行,因为我必须以XML的形式返回响应主体,因为它可能不是错误,而是必须反序列化的对象。 This particular side of things, I cannot change. 这方面的事情,我无法改变。

Any advice would be very much appreciated. 任何建议将非常感谢。 - Eli - 伊莱

EDIT: Just wanted to clarify that my basic code is working okay for non-error situations, and is deserializing the XML just fine. 编辑:只是想澄清我的基本代码在非错误情况下正常工作,并且正确地反序列化XML。 It's when my code encounters a HTTP 400 or an HTTP 500 error, where accessing the XML from the catch statement becomes a problem, because my code immediately throws an exception. 当我的代码遇到HTTP 400或HTTP 500错误时,从catch语句访问XML会成为问题,因为我的代码会立即抛出异常。

The body of a HTTP message (the XML in your case) can be retrieved with the GetResponseStream method of the HttpWebResponse object you have. 可以使用您拥有的HttpWebResponse对象的GetResponseStream方法检索HTTP消息的主体(在您的情况下为XML)。 And, since it's a stream, you can for instance read it with a StreamReader , like so: 而且,因为它是一个流,你可以用StreamReader读取它,如下所示:

HttpWebResponse myWebResponse; // Get this from whereever you want

Stream responseStream = myWebResponse.GetResponseStream();

StreamReader reader = new StreamReader(responseStream);
string niceStringForYou = reader.ReadToEnd();

...and from that point on, you can do whatever to it. ......从那时起,你可以做任何事情。

If you're absolutely sure it's always gonna be XML you get back from the service, you can probably even use an XmlReader to get XML directly from the stream: 如果您完全确定它将始终是XML,那么您甚至可以使用XmlReader直接从流中获取XML:

XmlReader foo = XmlReader.Create(responseStream);

Comment to edit: As long as you have the HttpWebResponse object, reading it's response stream ( GetResponseStream() ) should work. 编辑注释:只要你有HttpWebResponse对象,读取它的响应流( GetResponseStream() )应该有效。 And as you point out in your own code, you can get the HttpWebResponse by looking at (HttpWebResponse)exceptionParameter.Response . 正如您在自己的代码中指出的那样,您可以通过查看(HttpWebResponse)exceptionParameter.Response来获取HttpWebResponse。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM