C#HttpWebRequest服务器未返回完整响应 [英] C# HttpWebRequest Server not returning full response

查看:148
本文介绍了C#HttpWebRequest服务器未返回完整响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向服务器返回HTTP请求,该服务器返回带有数据的HTML.但是有时它停在中间"而没有任何明确的解释. 例如响应结束:

I am making a HTTP request to an server that returns HTML with data. But sometimes it "stops in the middle" without any clear explanation. For example the end of response:

[...Content length 14336 chars ...]</tbody></table><p /><br /><ul id=

它只是在生成HTML的过程中停止. 我的代码:

It simply stops in the middle of generating the HTML. My code:

var request = (HttpWebRequest) WebRequest.Create("http://example.com);
var authInfo = string.Format("{0}:{1}", "username", "password");
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers.Add("Authorization", "Basic " + authInfo);
request.Credentials = new NetworkCredential(user.Xname, decryptedPassword);
request.Method = WebRequestMethods.Http.Get;
request.AllowAutoRedirect = true;
request.Proxy = null;
var response = (HttpWebResponse) request.GetResponse();
var stream = response.GetResponseStream();
var streamreader = new StreamReader(stream, Encoding.GetEncoding("ISO-8859-2"));
var s = streamreader.ReadToEnd();

代码是否有可能不等到最后?

Is it somehow possible that the code is not waiting until the end?

推荐答案

在本地执行http Web请求(调试)时,几乎没有网络延迟,一切都很好,但是,当您在Internet上使用相同的代码时,服务器未将所有响应数据发送到一个块中,服务器可能正在忙于处理其他请求,或者可能存在一些网络延迟,那么您将收到多个数据块中的响应数据.

When you are executing a http web request locally (debugging) there almost no network delay and everything is fine, however when you are using the same code on internet the servers does not send all response data in one chunk, server could be busy processing other requests or there could be some network latency, then, you receive the response data in several chunks.

StreamReader的ReadToEnd方法将仅在您将第一块数据接收到流中时读取可用数据,而不会等待其他数据.

The ReadToEnd methods from StreamReader will read just the data available when you received the first chunk of data into the stream, and won't wait for further data.

这并不意味着响应已完成,并且您已经收到所有响应数据,在Internet上某处找到的下一个代码可以正确处理读取过程...(该代码不是我的,我无法获得赞誉)

This not means the response is complete and you have received all response data, next code found somewhere on internet handle the read process properly... (code is not mine and I cannot get the credit for it)

    public static byte[] ReadFully(Stream stream, int initialLength)
    {
        // If we've been passed an unhelpful initial length, just
        // use 32K.
        if (initialLength < 1)
        {
            initialLength = 32768;
        }


        byte[] buffer = new byte[initialLength];
        int read = 0;


        int chunk;
        while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
        {
            read += chunk;


            // If we've reached the end of our buffer, check to see if there's
            // any more information
            if (read == buffer.Length)
            {
                int nextByte = stream.ReadByte();


                // End of stream? If so, we're done
                if (nextByte == -1)
                {
                    return buffer;
                }


                // Nope. Resize the buffer, put in the byte we've just
                // read, and continue
                byte[] newBuffer = new byte[buffer.Length * 2];
                Array.Copy(buffer, newBuffer, buffer.Length);
                newBuffer[read] = (byte)nextByte;
                buffer = newBuffer;
                read++;
            }
        }
        // Buffer is now too big. Shrink it.
        byte[] ret = new byte[read];
        Array.Copy(buffer, ret, read);
        return ret;
    }

这篇关于C#HttpWebRequest服务器未返回完整响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆