[...]
I am new to these technologies. Your approach seems interesting. Do
you think with your approach there is a way to post the data and then
read the binary data?
AFAIK, WebClient does not support POST requests directly. It is intended
for simpler types of queries.
However, you can use the WebRequest class directly. It is not much more
complicated, and does allow for POST requests. The data is still
available as soon as it's received by the client; contrary to the
assumption you alluded to in a previous post, it is not true that all of
the data must have arrived before any of it is available in the stream.
See below for a concise-but-complete code example that illustrates the
use of both WebClient and WebRequest for the purpose of receiving
streamed data. (Note that the code is for illustration purposes only,
containing no real error handling and recovery, nor other niceties one
might find in a real program).
I think it's possible that you could sub-class WebClient, overriding the
protected GetWebRequest() method so that you can convert the request to
the necessary POST request you want. But IMHO it's easier to just use
WebRequest directly at that point, rather than to try to get WebClient
to do what you want.
Pete
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
namespace TestWebStreaming
{
class Program
{
static void Main(string[] args)
{
using (LoopingServer server = new LoopingServer())
{
WebRequest request =
WebRequest.Create("
http://localhost:8888/");
using (WebResponse response = request.GetResponse())
{
_ReadResponseStream(response.GetResponseStream());
}
using (WebClient client = new WebClient())
{
_ReadResponseStream(client.OpenRead("
http://localhost:8888/"));
}
}
}
private static void _ReadResponseStream(Stream stream)
{
using (StreamReader reader = new StreamReader(stream))
{
Debug.WriteLine("connected to server");
string strLine;
while ((strLine = reader.ReadLine())!= null)
{
Console.WriteLine(strLine);
}
}
}
}
class LoopingServer : IDisposable
{
private HttpListener _listener;
public LoopingServer()
{
_listener = new HttpListener();
_listener.Prefixes.Add("
http://localhost:8888/");
_listener.Start();
_listener.BeginGetContext(_Connected, _listener);
}
private void _Connected(IAsyncResult ar)
{
try
{
HttpListener listener = (HttpListener)ar.AsyncState;
HttpListenerContext context = listener.EndGetContext(ar);
StreamWriter writer = new
StreamWriter(context.Response.OutputStream);
Debug.WriteLine("connected client");
System.Timers.Timer timer = new System.Timers.Timer(1000);
int clines = 0;
timer.Elapsed += (sender, e) =>
{
if (clines++ < 10)
{
string strLine= DateTime.Now.ToString();
Debug.WriteLine("sending line \"{0}\"",
(object)strLine);
writer.WriteLine(strLine);
writer.Flush();
}
else
{
Debug.WriteLine("closing client");
timer.Stop();
writer.Dispose();
}
};
timer.Start();
listener.BeginGetContext(_Connected, listener);
}
catch (Exception exc)
{
Debug.WriteLine("ERROR: {0}: \"{1}\"",
exc.GetType().Name, exc.Message);
}
}
public void Dispose()
{
if (_listener != null)
{
_listener.Close();
_listener = null;
}
}
}
}