❔ HttpClient exiting program during GetStringAsync() and GetAsync()

I'm sending a GET to an API, but when debugging, as soon as you step off the last line, it cuts out the program without returning any value. Does anyone have any ideas?
40 Replies
Angius
Angius2y ago
Try wrapping it in a try/catch and see if there's some exception happening?
Japemaster Brad
Japemaster BradOP2y ago
No exception thrown, just a clean exit. I've come across this before, I think it was something about using .GetAwaiter().GetResult(), but that doesn't seem to give me much I just used .WaitAsync() in try/catch and it threw an error for timing out
Auger
Auger2y ago
Nah, you're awaiting, which is what you should be doing Are you hitting the console line?
Japemaster Brad
Japemaster BradOP2y ago
Cuts out as I step onto it
Auger
Auger2y ago
So, it isn't making it past line 17?
Japemaster Brad
Japemaster BradOP2y ago
No
Auger
Auger2y ago
The timeout exception you saw, could indicate the issue. If this is a GET request, you can try doing the same request from either a browser or postman to look into it You may want to ensure the API is reachable at all Try pasting the url you're trying to hit in your code, inside your browser
Japemaster Brad
Japemaster BradOP2y ago
I actually built it in Postman first, then transferred all the data into VS, that's what confused me at first, it's almost like it's getting the response, ticking the box and cutting out
Auger
Auger2y ago
You'll likely see a 401 since you won't have a bearer, but that at least means the API is reachable To me, it sounds like the async method isn't resolving and hanging until the default http timeout is hit So, I'd double check your requestUri Especially since you're concatenating strings to make it
Japemaster Brad
Japemaster BradOP2y ago
Combed over it, and nothing 😦
Auger
Auger2y ago
Try setting a breakpoint on line 17, and take that URL and post it into your browser. I would expect a 401, and it not to time out
Japemaster Brad
Japemaster BradOP2y ago
Auger
Auger2y ago
Yeah, so that's what I'd expect. So does it resolve in postman when you pass the bearer token?
Japemaster Brad
Japemaster BradOP2y ago
I get a 200 with JSON if that's what you mean 🙂
Auger
Auger2y ago
Hmm.. You might need more headers in your httpclient in C#
Japemaster Brad
Japemaster BradOP2y ago
Honestly that could be it, I'll have a look at the documentation again and have a read. Will get back to you with findings, thanks for your help!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
demidev_mb
demidev_mb2y ago
Disable just my code in debug settings, and enable all exception types so visual studio cache everything retry and see what's actually happening.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
demidev_mb
demidev_mb2y ago
Yeah, try cache is required anyway
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
is this the thing where the console closes after finishing everything there is a setting for that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
ah
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
i'll leave you this for the future so you can log all http stuff to console
demidev_mb
demidev_mb2y ago
And don't forget to global exception events, they can help
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
public class LoggingHandler : DelegatingHandler
{
public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine("Request:");
Console.WriteLine(request.ToString());
if (request.Content != null)
{
Console.WriteLine(await request.Content.ReadAsStringAsync());
}
Console.WriteLine();

HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();

return response;
}
}
public class LoggingHandler : DelegatingHandler
{
public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine("Request:");
Console.WriteLine(request.ToString());
if (request.Content != null)
{
Console.WriteLine(await request.Content.ReadAsStringAsync());
}
Console.WriteLine();

HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();

return response;
}
}
HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler()));
HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler()));
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
just for quick glancing nothing more
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
it doesnt consume
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
it says in the docs it internally buffers it with LoadIntoBufferAsync() then copies it
Japemaster Brad
Japemaster BradOP2y ago
The program exits out normally, same as before
✿ Scarlet ✿
Did you try to execute mocking the value? are you sure that the issue is related with HttpClient?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Tvde1
Tvde12y ago
Are you sure everything is awaited? So not:
public static void Main()
{
DoStuff();
}

public static async Task DoStuff()
public static void Main()
{
DoStuff();
}

public static async Task DoStuff()
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server