Posts

Showing posts from September, 2008

Creating an HTTP Streaming Client in Silverlight

I read and played with WebClient and HttpWebRequest classes. Both of these classes allow you to make requests to an http server. Once you make the request, you give a callback that is invoked once the response is received so you can happily examine the response. Let's say your HTTP server is streaming a file, or it wants to hold on to your request and sends messages as necessary from the same connection. To be able to work with such a server, your http client must be able to read the data as soon as it is available without requiring the response to be received completely.  After searching silverlight forums I found a solution. The solution assumes that you have a control on the server code or the response you are getting is bigger than 4KB (IE buffers upto 4KB). Here is the code snipped for your client side: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "POST"; request.AllowReadStreamBuffering = false; try {  request.BeginGetResponse(On

HTTP Streaming Ruby Client

Let's say you have a HTTP streaming server such as the one below where the server sends the HTTP response slowly or may it even holds on the request and does not close the connection and keep sending data (so called "HTTP Streaming"). How would you write a client that receives the response segments as soon as they arrive? (we do not want to wait till the whole response ends or we do not have a choice since the server is streaming "infinite" data). require 'rubygems' require 'mongrel' class StreamHandler <>   def process(request, response)     response.status = 200     response.send_status(nil)     response.header['Content-Type'] = "text/plain"      response.send_header     10.times do       response.write("Message \r\n")       sleep 5     end     response.done   end end Mongrel::Configurator.new do   listener :port => 3000 do     uri "/", :handler => StreamHandler.new   end   run; join end The cli