Search code examples
c#dotnet-httpclient

Handling HttpResponseMessage with persistent connection c #


I am using Httpclient C # .net to connect to a url.

I need to make a GET request. This connection is persistent. The problem is that I need to perform actions according to the notifications that the server sends me.

When executing the HttpResponseMessage, it hangs (being persistent it works and receiving notifications constantly, but it never gives me back control to be able to work according to the notifications of the server) when canceling the execution I can see all notifications by console.

Is there any way to control HttpResponseMessage to be able to work with every response the server sends me?

Should I use another type of technology for this?

This is what I get back by console de "await response.Content.ReadAsStringAsync ();" when I cancel the operation.

<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<message xmlns = "http://asdasd.com/asd/08/DS/Sync" xmlns: ns2 = "http: // asdadasdasdasd">
<event> KEEPALIVE </ event>
</ message>

<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<message xmlns = "http://asdasd.com/asd/08/DS/Sync" xmlns: ns2 = "http: // asdadasdasdasd">
<event> KEEPALIVE </ event>
</ message>

<? xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<message xmlns = "http://asdasd.com/asd/08/DS/Sync" xmlns: ns2 = "http: // asdadasdasdasd">
<event> KEEPALIVE </ event>
</ message>

every ('message') arrives approximately every 2 minutes I wish that each time I get one I can perform an action I leave the code that ultilizo. Thank you

public async static Task<int> GetRequest(string url)
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = new HttpResponseMessage();
    response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
    Console.WriteLine("STATUS OF CONNECTION");
    Console.WriteLine(response.StatusCode);
    response.EnsureSuccessStatusCode();
    var body = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false); ;

    Console.WriteLine("ANSWER");
    Console.WriteLine(body);
    Console.WriteLine(" \n");
    Console.WriteLine("\n ATTENTION!! Disconnected from the Persistence line \n");

    Console.WriteLine("Connections done");
    int result1 = 1;
    return result1;
}

Solution

  • You are looking to parse partial HTTP responses, as and when each part is received. The HTTP protocol does not know/understand that you are sending/receiving multiple discrete messages separated by a newline in a single HTTP response.

    As far as I know, HttpClient won't be much help to you because it is designed to receive one whole HTTP response. Rick Strahl has a blog on exactly this subject Using .NET HttpClient to capture partial Responses.

    You should be able to use HttpWebRequest to manually read bytes from the network stream into a buffer. After appending to the buffer, you will want to check if it contains a complete message (<message> ... </ message>). If so, convert the message to a string and announce it as required: eg raise an event, start a task, call a method, add to a queue. Then clear the buffer and repeat.

    TcpClient is probably not the best approach in this scenario, because you may then also need to implement TLS (if your endpoint is HTTPS).