.NET Framework Developer Center > .NET Development Forums > .NET Framework Networking and Communication > I can´t make HTTP pipelining work with .net 2.0 HttpListener and HttpWebRequest
Ask a questionAsk a question
 

QuestionI can´t make HTTP pipelining work with .net 2.0 HttpListener and HttpWebRequest

  • Wednesday, October 31, 2007 3:04 AMDamian L Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

     

    I need to achieve high speed communication between two servers using http with only one TCP connection. If I don't limit the connections it uses as many connections as it needs to send all the requests. Limiting the connections it uses only one (or as many as I want). It seems to reuse the connections, but the pipelining is not working (it sends only one request at a time on each connection)

     

    You can see with this example is that it sends the next request only after the last one was replied, so the pipelining it's not working (I see that the client gets only one response every 10 seconds).

     

    Here is the server and client code:

     

    // Client that sends 10 requests per second

    // I force it to use only one connection.

    ServicePointManager.DefaultConnectionLimit = 1;

    while(true)

    {

    HttpWebRequest wr = (HttpWebRequest) WebRequest.Create("http://damian:8080/coco/" + i.ToString());

    i++;

    wr.Pipelined = true;

    wr.Method = "GET";

    wr.BeginGetResponse(new AsyncCallback(ProcesarResponseEnviado), wr);

    Thread.Sleep(100);

    }

     

    void ProcesarResponseEnviado(IAsyncResult ia)

    {

    WebResponse wrp = ((WebRequest) ia.AsyncState).EndGetResponse(ia);

    Stream streamResponse = wrp.GetResponseStream();

    StreamReader streamRead = new StreamReader(streamResponse);

    string responseString = streamRead.ReadToEnd();

    Console.WriteLine(responseString);

    // Close the stream object.

    streamResponse.Close();

    streamRead.Close();

    // Release the HttpWebResponse.

    wrp.Close();

    }

     

     

    // Server that takes 10 seconds to proccess each request

    hl = new HttpListener();

    hl.Prefixes.Add("http://damian:8080/");

    hl.Start();

    hl.BeginGetContext(new AsyncCallback(ProcesarRequestRecibido), hl);

     

    void ProcesarRequestRecibido(IAsyncResult result)

    {

    HttpListener listener = (HttpListener)result.AsyncState;

    HttpListenerContext context = listener.EndGetContext(result);

    HttpListenerRequest request = context.Request;

    hl.BeginGetContext(new AsyncCallback(ProcesarRequestRecibido), hl);

    // Obtain a response object.

    HttpListenerResponse response = context.Response;

    // Simulates a complicated operation that takes 10 seconds.

    Thread.Sleep(10000);

    // Construct a response.

    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";

    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);

    // Get a response stream and write the response to it.

    response.ContentLength64 = buffer.Length;

    System.IO.Stream output = response.OutputStream;

    output.Write(buffer, 0, buffer.Length);

    // You must close the output stream.

    output.Close();

    }