2
我正在为Web服务的客户端工作,该服务返回标准状态代码(404,200等) 以及定制的json消息。C#System.Net.WebException检索自定义404消息
我一直未能找到包含自定义消息的WebException属性。
关于如何获取消息的任何想法?
我正在为Web服务的客户端工作,该服务返回标准状态代码(404,200等) 以及定制的json消息。C#System.Net.WebException检索自定义404消息
我一直未能找到包含自定义消息的WebException属性。
关于如何获取消息的任何想法?
简单的解决方案:
将引发WebException在响应属性中的实际WebResponse的。 从这里仅仅是处理此回应按正常的事:
private static JsonParseException ProcessException(WebException webEx)
{
var stream = webEx.Response.GetResponseStream();
using (var memory = new MemoryStream())
{
var buffer = new byte[4096];
var read = 0;
do
{
read = stream.Read(buffer, 0, buffer.Length);
memory.Write(buffer, 0, read);
} while (read > 0);
memory.Position = 0;
int pageSize = (int)memory.Length;
byte[] bytes = new byte[pageSize];
memory.Read(bytes, 0, pageSize);
memory.Seek(0, SeekOrigin.Begin);
string data = new StreamReader(memory).ReadToEnd();
memory.Close();
DefaultMeta meta = JsonConvert.DeserializeObject<DefaultMeta>(data);
return new JsonParseException(meta.Meta, meta.Meta.Error, webEx);
}
}
我使用NewtonSoft JSON库,以deserialise JSON响应
你能粘贴代码吗? – shahkalpesh 2009-12-01 06:03:59
这不是代码特定的 - 这是一个普遍的问题。 – 2009-12-01 06:05:43