You probably do not want to use a StreamReader
stream to read WebResonse
unless you know that the stream contains newline characters. StreamReader
like to think in terms of lines, and if there are no new lines in the stream, it will hang.
It is best to read as many bytes as you want in a buffer byte[]
, and then convert it to text. For instance:
int BYTES_TO_READ = 1000;
var buffer = new byte[BYTES_TO_READ];
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
using (Stream sm = resp.GetResponseStream())
{
int totalBytesRead = 0;
int bytesRead;
do
{
bytesRead = sm.Read(buffer, totalBytesRead, BYTES_TO_READ-totalBytesRead);
totalBytesRead += bytesRead;
} while (totalBytesRead < BYTES_TO_READ);
request.Abort();
}
}
At this point, the buffer has the first BYTES_TO_READ
bytes from the buffer. Then you can convert this to a string, for example:
string s = Encoding.Default.GetString(buffer);
MemoryStream
, StreamReader
.
WebResponse
, . , , , , request.Abort()
, . .
, , " ", "".