0

I have a curl call that will connect to a data stream and return a value every time a new one is posted to the data stream.

curl -XPOST -d uuid=\"94701e51-a24f-42b8-b8fd-81e0ecb79a2b\" http://bms.org/republish

I've tried making a HttpClient post that generates the same http request using this code

Main:

static async Task Main(string[] args)
{
    Republish r = new Republish(); //The class name.
    try
    {
        Task.Run(() => r.Test()).Wait();
        Console.WriteLine("bla");
        Console.ReadKey();
    }
    catch (Exception ex)  //Exceptions here or in the function will be caught here
    {
        Console.WriteLine("Exception: " + ex.Message);
    }
}

public async Task<string> Test()
{
    var client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
    client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("curl", "7.48.0"));
    client.DefaultRequestHeaders.ExpectContinue = false;

    var requestContent = new StringContent("uuid=\"94701e51-a24f-42b8-b8fd-81e0ecb79a2b\"", Encoding.UTF8, "application/x-www-form-urlencoded");
    HttpResponseMessage response = await client.PostAsync("http://bms.org/republish", requestContent);
    HttpContent responseContent = response.Content;

    using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
    {
        Console.WriteLine(await reader.ReadToEndAsync()); 
    }
    //I never got to the return statement because it breaks.
}

But this never yields anything. After some searching around I think it's because await client.PostAsync is waiting for the entire payload to be received before it streams it.

How can I stream it in real time?

Igor
  • 60,821
  • 10
  • 100
  • 175
Ehrendil
  • 233
  • 3
  • 13
  • Or its because of a deadlock in your async/await code. Are you calling mixing `.Result` with `async/await` in this call stack? – Igor Dec 17 '18 at 21:06
  • @Igor I edited the code to show the full code. – Ehrendil Dec 17 '18 at 21:10
  • 1
    https://stackoverflow.com/q/17248680/1260204 – Igor Dec 17 '18 at 21:13
  • 1
    [Why does this async action hang?](https://stackoverflow.com/questions/14526377/why-does-this-async-action-hang) – Igor Dec 17 '18 at 21:15
  • @Igor how can I run that from a public static void main? – Ehrendil Dec 17 '18 at 21:17
  • 1
    [Can't specify the 'async' modifier on the 'Main' method of a console app](https://stackoverflow.com/a/44254451/1260204) – Igor Dec 17 '18 at 21:18
  • I edited the code in the Main (edit shows), but it still hangs on PostAsync. – Ehrendil Dec 17 '18 at 21:28
  • `Task.Run(() => r.Test()).Wait();` should be `await r.Test();` – Igor Dec 17 '18 at 21:30
  • It's very much the same, unfortunately. :/ – Ehrendil Dec 17 '18 at 21:33
  • The exception message is "A task was cancelled." – Ehrendil Dec 17 '18 at 21:38
  • `//I never got to the return statement because it breaks.` and `The exception message is "A task was cancelled."` are 2 very different things. You *had* a dead lock and *after* you altered the code you have an Exception. Start doing the usual investigation to figure out why you have that exception like debugging, reading the stack trace, reading nested exceptions, and checking the server side log. – Igor Dec 17 '18 at 21:47
  • But "it breaks" was referring to the same exception, unfortunately. The request would eventually time out and return "A task was cancelled". The server logs don't show any sign of this call. – Ehrendil Dec 17 '18 at 21:49
  • It seems that the task is getting cancelled. – Ehrendil Dec 17 '18 at 21:58

0 Answers0