C
C#

help

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

JBJapemaster Brad2/3/2023
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?
AAngius2/3/2023
Try wrapping it in a try/catch and see if there's some exception happening?
JBJapemaster Brad2/3/2023
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
AAuger2/3/2023
Nah, you're awaiting, which is what you should be doing Are you hitting the console line?
JBJapemaster Brad2/3/2023
Cuts out as I step onto it
AAuger2/3/2023
So, it isn't making it past line 17?
JBJapemaster Brad2/3/2023
No
AAuger2/3/2023
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
JBJapemaster Brad2/3/2023
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
AAuger2/3/2023
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
JBJapemaster Brad2/3/2023
Combed over it, and nothing 😦
AAuger2/3/2023
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
JBJapemaster Brad2/3/2023
AAuger2/3/2023
Yeah, so that's what I'd expect. So does it resolve in postman when you pass the bearer token?
JBJapemaster Brad2/3/2023
I get a 200 with JSON if that's what you mean 🙂
AAuger2/3/2023
Hmm.. You might need more headers in your httpclient in C#
JBJapemaster Brad2/3/2023
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!
UUUnknown User2/3/2023
15 Messages Not Public
Sign In & Join Server To View
Ddemidev_mb2/3/2023
Disable just my code in debug settings, and enable all exception types so visual studio cache everything retry and see what's actually happening.
UUUnknown User2/3/2023
5 Messages Not Public
Sign In & Join Server To View
Ddemidev_mb2/3/2023
Yeah, try cache is required anyway
UUUnknown User2/3/2023
Message Not Public
Sign In & Join Server To View
HHenkypenky2/3/2023
is this the thing where the console closes after finishing everything there is a setting for that
UUUnknown User2/3/2023
6 Messages Not Public
Sign In & Join Server To View
HHenkypenky2/3/2023
ah
UUUnknown User2/3/2023
Message Not Public
Sign In & Join Server To View
HHenkypenky2/3/2023
i'll leave you this for the future so you can log all http stuff to console
Ddemidev_mb2/3/2023
And don't forget to global exception events, they can help
UUUnknown User2/3/2023
5 Messages Not Public
Sign In & Join Server To View
HHenkypenky2/3/2023
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()));
UUUnknown User2/3/2023
3 Messages Not Public
Sign In & Join Server To View
HHenkypenky2/3/2023
just for quick glancing nothing more
UUUnknown User2/3/2023
7 Messages Not Public
Sign In & Join Server To View
HHenkypenky2/3/2023
it doesnt consume
UUUnknown User2/3/2023
2 Messages Not Public
Sign In & Join Server To View
HHenkypenky2/3/2023
it says in the docs it internally buffers it with LoadIntoBufferAsync() then copies it
JBJapemaster Brad2/4/2023
The program exits out normally, same as before
RSRaven Scarlet2/4/2023
Did you try to execute mocking the value? are you sure that the issue is related with HttpClient?
UUUnknown User2/4/2023
3 Messages Not Public
Sign In & Join Server To View
TTvde12/4/2023
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()
AAccord2/5/2023
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
✅ Homework Helphttps://i.gyazo.com/f23d758d46581bcfb84f88d4178cd484.png What should I return from the FillArray() ❔ force a ushort value?if i type for example var num = 25L; i am forcing a long value (int64) how can i force a ushort in ❔ Doing post method with entity frameworki'm making a simple API to practice with entity framework but when i try to make a post to a table w❔ I can't reference a class library please helpI can't reference a class library i t says compilation error❔ ASP.NET REMOTE DEVELOPMENTHey does anyone know if I would need nginx for remote development on a ubuntu server from my windowException/NULL/Error handling advice.DeserializeAsync clearly returns a nullable generic object. From the picture we can clearly see that❔ ASP.NET Core web app vs ASP.net web application(.NET Framework)Hi, Ive been working with c# for a while now and would like to make a webapp but I dont know where t✅ IGNORE PEOPLEmy place for testing commands❔ Game Hubhi, how can i program my own game hub, like battle.net where i can download and start my own games ?❔ ✅ beginner class and propertiesi am doing basic program abt class and getting information from input but on the weight section whe❔ Best practice to retrieve MySQL columnsHello wonderful people! Can you please help me understanding what would be the best practice to ret❔ Auto Slide Showhere my code✅ materialDesign:Cardfor some reason it says that closing matrialcard doesent exist and it shows an error❔ Why does this code give the CS8604 warning?I'm confused why the below code gives the CS8604 warning. As far as I am concerned, I am null checki✅ Having trouble adding a picture to my code.what should the path look like?❔ Producer Consumer ISourceBlock<T>, periodically flushing consumed dataHi, I've the following scenario: I've a camera continuously acquiring frames and a c# app receiving ❔ Load async data and display it (WindowsAppSDK / WinUI3)I am currently developing my first windows app with C#, Windows App SDK and WinUI3. I want to do som❔ Best Approach to Implement Authentication and Authorization in Microservices with ASP.NET CoreHi friends, I'm looking for experience share. I have Microservices architecture project, that uses ❔ Performance of many small requests vs less but bigger requestsI have 2 projects: an API hosted on aws and a desktop application. Currently, I'm fetching around 4k✅ Image from dataurlI have dataurl with image and i need to get it to some reasonable image object in c#, so i can resiz