❔ 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
Try wrapping it in a
try/catch
and see if there's some exception happening?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 outNah, you're awaiting, which is what you should be doing
Are you hitting the console line?
Cuts out as I step onto it
So, it isn't making it past line 17?
No
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
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
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 itCombed over it, and nothing 😦
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
Yeah, so that's what I'd expect. So does it resolve in postman when you pass the bearer token?
I get a 200 with JSON if that's what you mean 🙂
Hmm.. You might need more headers in your httpclient in C#
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•2y ago
Message Not Public
Sign In & Join Server To View
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•2y ago
Message Not Public
Sign In & Join Server To View
Yeah, try cache is required anyway
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
is this the thing where the console closes after finishing everything
there is a setting for that
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
ah
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
i'll leave you this for the future so you can log all http stuff to console
And don't forget to global exception events, they can help
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
just for quick glancing nothing more
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
it doesnt consume
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
it says in the docs it internally buffers it
with LoadIntoBufferAsync()
then copies it
The program exits out normally, same as before
Did you try to execute mocking the value?
are you sure that the issue is related with HttpClient?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Are you sure everything is awaited?
So not:
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.