-1

I've been trying for a day to make this work synchronously, not async.

Here is the code that works:

HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = false;
handler.Credentials = new NetworkCredential(username, password);
                 
HttpClient clientPageFirst = new HttpClient(handler);

HttpResponseMessage responsePageFirst = await clientPageFirst.GetAsync(fullURL);
HttpContent contentPageFirst = responsePageFirst.Content;

string resultPageFirst = await contentPageFirst.ReadAsStringAsync();
Console.WriteLine(resultPageFirst);

It's for a C# console app, and there is another call there to another platform's API which works synchronously, but it uses tokens in the header to validate, not a network credential like this one (calling a local on premise CRM URL).

Can someone please help me change the

HttpResponseMessage responsePageFirst = await clientPageFirst.GetAsync(fullURL);

line so it is synchronous?

T.I.A.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Rodney Ellis
  • 727
  • 2
  • 13
  • 28
  • could you please explain what exactly the _problem_ is with running async? – Franz Gleichmann Jun 16 '21 at 05:52
  • well, if you need to know, it's because 1. it's a console app, 2. i need to loop though pages,and save each page and 3. there is another call to a different API in the same app which works fine in sync. and finally, because it's easier doing it all in a console app when it's in sync – Rodney Ellis Jun 16 '21 at 05:55
  • 1
    may be he doesn't want to change the method signature to async/Task etc? – Sekhar Jun 16 '21 at 05:55
  • 2
    Have you considered changing your `Main` method to be `async`? Is that an option? – mjwills Jun 16 '21 at 06:03
  • Does this answer your question? [How to call asynchronous method from synchronous method in C#?](https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c) – devsmn Jun 16 '21 at 06:09
  • 2
    @RodneyEllis sorry, but i just don't see how any of those points contradicts with using async methods. – Franz Gleichmann Jun 16 '21 at 06:16
  • 1
    "to another platform's API which works synchronously", the `await` usage ensure execution flow await the results at that line.. so if the call your making doesn't support async/task await then you would just substitute that line with the synchronous call. – Brett Caswell Jun 16 '21 at 06:34
  • 2
    after looking at the accepted answer and commenting to effect, you should update your snippet to include the entire method signature for this scope, and reflect on your intention to have that method not be `async`. *though I still think the problem with this question is that your approach is generally unrecommended, and that you should consider an approach to call this other platform's API asynchronously.* – Brett Caswell Jun 16 '21 at 06:45

1 Answers1

1

Try this HttpResponseMessage responsePageFirst = clientPageFirst.GetAsync("fullURL").Result;

Sekhar
  • 5,614
  • 9
  • 38
  • 44
  • thanks, that kinda works, but how do i read the result into resultPageFirst ? ie. the ReadAsStringAsync() function now needs to change. can you add that too please? – Rodney Ellis Jun 16 '21 at 06:05
  • 1
    you can do `string resultPageFirst = contentPageFirst.ReadAsStringAsync().Result;` you can call (dot).Result to make the calls sync – Sekhar Jun 16 '21 at 06:09
  • how is this different then the current implementation that does `await`? ...or was it based on the presumption that the scope of this method was removing `async`? – Brett Caswell Jun 16 '21 at 06:36
  • in the snippet of code that the OP provided., What additional benefit does using the aync/await provide? – Sekhar Jun 16 '21 at 07:43
  • 1
    Stephen C's Blog: [Don't block on async code](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html) – Fildor Jun 16 '21 at 08:29