我有一个C#应用程序和一个PHP API。当API收到错误请求时,我返回406 Not Acceptable错误,并带有描述性消息,说明请求不可接受的原因。
当我在C#中捕获WebException时,如何获取我的消息,以便我可以获取描述性消息,而不是“远程服务器返回错误:(406)Not Acceptable”。在WebException.Message
中响应看起来像这样
<?xml version="1.0" encoding="UTF-8" ?>
<resultxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<code>406</code>
<message>
<![CDATA[You just did something that is not acceptable.]]>
</message>
<errorsxsi:nil="true"/>
</result>
这是带有WebException catch的C#
HttpWebRequest _Request = (HttpWebRequest)WebRequest.Create(url);
_Request.Method = "PUT";
_Request.Timeout = 100000;
_Request.ContentType = "text/xml";
try
{
return (HttpWebResponse)_Request.GetResponse();
}
catch (WebException _Exception)
{
MessageBox.Show("WebException raised :" + _Exception.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
我应该用?
替换_Exception.Message