Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#System.Net.WebException检索自定义404消息_C#_System.net.webexception - Fatal编程技术网

C#System.Net.WebException检索自定义404消息

C#System.Net.WebException检索自定义404消息,c#,system.net.webexception,C#,System.net.webexception,我正在为一个返回标准状态码(404200等)的Web服务开发一个客户端 以及自定义json消息 我无法找到包含自定义消息的WebException的属性 有没有关于如何获取信息的想法?简单解决方案: WebException在Response属性中具有实际的WebResponse。 从这里开始,只需按照正常方式处理响应: private static JsonParseException ProcessException(WebException webEx) {

我正在为一个返回标准状态码(404200等)的Web服务开发一个客户端 以及自定义json消息

我无法找到包含自定义消息的WebException的属性

有没有关于如何获取信息的想法?

简单解决方案:

WebException在Response属性中具有实际的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);
        }
    }
私有静态JsonParseException ProcessException(WebException webEx)
{
var stream=webEx.Response.GetResponseStream();
使用(var memory=new MemoryStream())
{
var buffer=新字节[4096];
var read=0;
做
{
read=stream.read(buffer,0,buffer.Length);
内存。写入(缓冲区,0,读取);
}while(read>0);
记忆位置=0;
int pageSize=(int)memory.Length;
字节[]字节=新字节[pageSize];
内存读取(字节,0,页面大小);
内存寻道(0,SeekOrigin.Begin);
字符串数据=新的StreamReader(内存).ReadToEnd();
memory.Close();
DefaultMeta=JsonConvert.DeserializeObject(数据);
返回新的JsonParseException(meta.meta、meta.meta.Error、webEx);
}
}

我正在使用NewtonSoft Json库对Json响应进行反序列化

请粘贴代码好吗?这不是特定于代码的-这是一个常见问题。