我正在向返回带有数据的HTML的服务器发出HTTP请求。但有时候它会在中间停止"没有任何明确的解释。 例如,回复结束:
[...Content length 14336 chars ...]</tbody></table><p /><br /><ul id=
它只是在生成HTML时停止。 我的代码:
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();
在某种程度上,代码可能不会等到结束?
答案 0 :(得分:1)
当您在本地执行http Web请求(调试)时,几乎没有网络延迟,一切都很好,但是当您在Internet上使用相同的代码时,服务器不会在一个块中发送所有响应数据,服务器可能是忙于处理其他请求或者可能存在一些网络延迟,然后,您会收到几个块的响应数据。
来自StreamReader的ReadToEnd方法将只读取您在流中收到第一个数据块时可用的数据,而不会等待更多数据。
这并不意味着响应已完成并且您已收到所有响应数据,在Internet上的某处找到的下一个代码正确处理读取过程...(代码不是我的,我无法获得它的信用)
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;
}