Unable to read data from the transport connection: Stream Reader Error
<p>Following code throws the error: <br/>
<i>"Unable to read data from the transport connection: The connection was closed."-System.IO.IOException.</i>
<br/>
This method receives a URL as parameter, execute it and gets response from webserver.
<br/>
<br/>While reading the response by using a Stream, the line <code>liBytesRead = lStream.Read(lBytes, 0, 128);</code> inside while loop throws the quoted error.</p>

<pre><code>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 &gt; 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;
}
}
</code></pre>

<p>Any idea why it is happening.....
<br/> Thanks.</p>
