C# 无法从传输连接读取数据:流读取器错误

C# 无法从传输连接读取数据:流读取器错误,c#,asp.net,C#,Asp.net,以下代码引发错误: “无法从传输连接读取数据:连接已关闭。”-System.IO.IOException。 此方法接收URL作为参数,执行它并从webserver获取响应。 使用流读取响应时,行liBytesRead=lStream.Read(lBytes,0,128)内部while循环抛出引用的错误 public bool GetFromUrl(ref string psUrl, ref string rsResult, ref int piTimeoutSeconds) { Syst

以下代码引发错误:
“无法从传输连接读取数据:连接已关闭。”-System.IO.IOException。
此方法接收URL作为参数,执行它并从webserver获取响应。

使用流读取响应时,行
liBytesRead=lStream.Read(lBytes,0,128)内部while循环抛出引用的错误

public bool GetFromUrl(ref string psUrl, ref string rsResult, ref int piTimeoutSeconds)
{
  System.Text.StringBuilder lStringB = new System.Text.StringBuilder();

try {
    WebRequest lWebRequest = WebRequest.Create(psUrl);
    int liTimeout = piTimeoutSeconds * 1000;

    lWebRequest.Timeout = liTimeout;

    WebResponse lWebResponse = lWebRequest.GetResponse;
    Stream lStream = default(Stream);
    lStream = lWebResponse.GetResponseStream;

    byte[] lBytes = new byte[129];
    int liBytesRead = lStream.Read(lBytes, 0, 128);
    System.Text.Encoding lEncode = System.Text.Encoding.GetEncoding("utf-8");

    while (liBytesRead > 0) {
        lStringB.Append(lEncode.GetString(lBytes, 0, liBytesRead));
        liBytesRead = lStream.Read(lBytes, 0, 128);
    }
    lStream.Close();

    rsResult = lStringB.ToString();
    return true;
} catch (System.IO.IOException e) {
    rsResult = e.ToString();
    return false;
}
}
知道为什么会这样吗。。。。。

谢谢。

看起来Web服务器在连接完成之前关闭了连接
试一试

详情请参阅:

HttpRequest.KeepAlive = false; 
HttpRequest.ProtocolVersion = HttpVersion.Version10;