Help with HTTPWebRequest stream
I'm trying to establish an open connection and then simply write out the stream contents on each iteration. If one were to enter the URL into a browser and refresh the page new data would show each time. I only wish to show each time-instance of data from the stream once (e.g. not 1 1,2 1,2,3 1,2,3,4 1,2,3,4,5 etc)
This is what I have to get the stream once, but the problem is after the first iteration it only prints newlines:
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create("http://www.google.com/finance/getprices?q=GOOG&x=NASD&i=120&p=25m&开发者_运维知识库;f=d,c,v,o,h,l&df=cpct&auto=1&ts=1308075761332");
myHttpWebRequest1.KeepAlive = true;
HttpWebResponse myHttpWebResponse1 = (HttpWebResponse)myHttpWebRequest1.GetResponse();
Stream streamResponse = myHttpWebResponse1.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
while (true)
{
Char[] readBuff = new Char[1000000];
int datalength = streamRead.Read(readBuff, 0, readBuff.Length);
String outputData = new String(readBuff, 0, datalength);
Console.Write(outputData + "\r\n");
Thread.Sleep(2000);
}
When you say "first iteration", are you referring to the first pass through the while-loop?
If so, then your buffer is 1,000,000 characters, but the Google page only has about 667 characters as a result of your query. I suspect that you're printing all the characters and then subsequent passes through the same loop will result in empty outputData
and CRLF (\r\n) (i.e. a bunch of new lines). In essence, the response stream is empty because you've read it all in the very first iteration of the while-loop, so you need to stop reading:
while (!streamRead.EndOfStream)
{
Char[] readBuff = new Char[1000000];
int datalength = streamRead.Read(readBuff, 0, readBuff.Length);
String outputData = new String(readBuff, 0, datalength);
Console.Write(outputData + "\r\n");
Thread.Sleep(2000);
}
Update:
I don't see how keep-alive can even work with HttpWebRequest, however, you can directly use a socket connection and construct the get request. I've written an asynchronous socket client that does that, so while it may not be exactly what you looking for I would still recommend you look at how the request is being constructed and sent.
private void BeginSend()
{
_clientState = EClientState.Sending;
Encoding ASCII = Encoding.ASCII;
string Get = "GET /" + _asyncTask.Path + " HTTP/1.1\r\nHost: " + _asyncTask.Host + "\r\nConnection: Keep-Alive\r\n\r\n";
byte[] buffer = ASCII.GetBytes(Get);
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.SetBuffer(buffer, 0, buffer.Length);
e.Completed += new EventHandler<SocketAsyncEventArgs>(SendCallback);
bool completedAsync = false;
try
{
completedAsync = _socket.SendAsync(e);
}
catch (SocketException se)
{
Console.WriteLine("Socket Exception: " + se.ErrorCode + " Message: " + se.Message);
}
if (!completedAsync)
{
// The call completed synchronously so invoke the callback ourselves
SendCallback(this, e);
}
}
The class has quite a bit of stuff that you may not need, but it's very robust. In your case, of course, you will not disconnect once the data has been read.
精彩评论