2

我有一个桌面客户端,它通过 Http 与服务器端通信。当服务器在数据处理方面出现一些问题时,它会在 Http 响应正文中使用正确的 Http 代码(主要是 HTTP-400)返回 JSON 中的错误描述。

当我阅读 HTTP-200 响应时,一切都很好,并且此代码有效:

 using (var response = await httpRequest.GetResponseAsync(token))
                {
                    using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                    {
                        return await reader.ReadToEndAsync();
                    }
                }

但是当发生错误并引发并捕获 WebException 时,会出现以下代码:

 catch (WebException ex)
            {
                 if (ex.Status == WebExceptionStatus.ProtocolError)
                {                   
                    using (var response = (HttpWebResponse) ex.Response)
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
                            {
                                var json = reader.ReadToEnd();
                            }
                        }
                    }
                }
            }

我已经对它做了一些事情以使其工作,但接下来发生了: response.ContentLength有效(184)但stream.Length为0,之后我无法读取json(它是"")我什至不知道在哪里看,因为一切看起来它应该工作。

可能是什么问题?

4

2 回答 2

4

经过一个月几乎每天的思考,我找到了解决方法。

问题是WebException.Response.GetResponseStream()返回的流与请求期间获得的流不完全相同(现在找不到指向 msdn 的链接),当我们捕获异常并读取此流时,实际的响应流已丢失(或类似那个,真的不知道并且无法在网上找到任何信息,除了查看现在是开源的 CLRCore)。

要在捕获之前保存实际响应,WebException您必须KeepAlive在您的属性上设置属性HttpRequest,瞧,您会在捕获异常时得到响应。所以工作代码如下所示:

            try
            {
                var httpRequest = WebRequest.CreateHttp(Protocol + ServerUrl + ":" + ServerPort + ServerAppName + url);

                if (HttpWebRequest.DefaultMaximumErrorResponseLength < int.MaxValue)
                    HttpWebRequest.DefaultMaximumErrorResponseLength = int.MaxValue;

                httpRequest.ContentType = "application/json";
                httpRequest.Method = method;
                var encoding = Encoding.GetEncoding("utf-8");

                if (httpRequest.ServicePoint != null)
                {
                    httpRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
                    httpRequest.ServicePoint.MaxIdleTime = 5000;
                }
                //----HERE--
                httpRequest.KeepAlive = true;
                //----------
                using (var response = await httpRequest.GetResponseAsync(token))
                {
                    using (var reader = new StreamReader(response.GetResponseStream(), encoding))
                    {                       
                        return await reader.ReadToEndAsync();
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    using (var response = (HttpWebResponse)ex.Response)
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
                            {
                                return reader.ReadToEnd();
                                //or handle it like you want
                            }
                        }
                    }
                }
            }

我不知道这样保持所有连接是否有效,但由于它帮助我读取服务器的实际响应,我认为它可能会帮助面临同样问题的人。

编辑:同样重要的是不要弄乱HttpWebRequest.DefaultMaximumErrorResponseLength.

于 2015-03-10T08:41:37.140 回答
0

我记得以前遇到过类似的问题,并且与设置流的位置有关。这是我之前为我工作的阅读 webResponse 的解决方案之一。请尝试类似的方法是否适合您:-

private ResourceResponse readWebResponse(HttpWebRequest webreq)
    {
        HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;
        HttpWebResponse webresp = null;// = webreq.GetResponse() as HttpWebResponse;
        var memStream = new MemoryStream();
        Stream webStream;
            try
            {
                webresp = (HttpWebResponse)webreq.GetResponse();
                webStream = webresp.GetResponseStream();
                byte[] readBuffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = webStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    memStream.Write(readBuffer, 0, bytesRead);
            }
            catch (WebException e)
            {
                var r = e.Response as HttpWebResponse;
                webStream = r.GetResponseStream();
                memStream = Read(webStream);
                var wrongLength = memStream.Length;
            }


            memStream.Position = 0;
            StreamReader sr = new StreamReader(memStream);
            string webStreamContent = sr.ReadToEnd();

            byte[] responseBuffer = Encoding.UTF8.GetBytes(webStreamContent);
//......
//.......

希望这可以帮助!

于 2015-02-05T08:09:49.740 回答