0

I'm trying to call a endpoint to get some info in my website but when I run the getAsync it's only works the first time in the current process.

static async Task Main(string[] args)
{

    await GetUserInfo("srmilton"); //Working
    await GetUserInfo("srmilton"); //Not Working

    while (true)
    {
        var res = await GetUserInfo("srmilton");
    }
}

public static async Task<(string,string)> GetUserInfo(string username)
{
    string url = "https://www.mywebsite.com/api/user/detail";
    var baseAddress = new Uri(url);

    using (var handler2 = new HttpClientHandler { UseCookies = false })
    using (var client2 = new HttpClient(handler2) { BaseAddress = baseAddress })
    {

        client2.DefaultRequestHeaders.Clear();
        client2.DefaultRequestHeaders.Add("Cookie", "session=zgbEqIjfSC7M7QdTTpHDkpWLt");
        client2.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
        AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
        client2.DefaultRequestHeaders.Add("x-requested-with", "XMLHttpRequest");
        client2.DefaultRequestHeaders.Add("referer", "https://www.mywebsite.com/");

        var result = await client2.GetAsync("");

        string responseString = await result.Content.ReadAsStringAsync();
        dynamic jsonresponse = JObject.Parse(responseString);
        id = jsonresponse.userInfo.user.id;
        sec_id = jsonresponse.userInfo.user.secUid;
        return (id, sec_id);
    }
}

The first time the fuction GetUserInfo is called it's return the correct json response from the api but the second loop gets stuck in GetAsync. I have already tried .ConfigureAwait(false), .Result and even creating HttpClient just once and reusing but it's aways hang on the second loop.

I don't know what i'm doing wrong, if someone can explain and show the right way to make this works i'll be thankful.

SrMilton
  • 45
  • 6
  • I strongly recommend you read these: https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ and https://josef.codes/you-are-probably-still-using-httpclient-wrong-and-it-is-destabilizing-your-software/ – ProgrammingLlama Jul 13 '22 at 02:50
  • I don't see anything obviously wrong in the question. Is the API that you're calling perhaps throttling the request? Does it return eventually? – ProgrammingLlama Jul 13 '22 at 02:51
  • I suggest temporarily removing your response handling and replace it with a request to `https://jsonplaceholder.typicode.com/todos/1` - does your code still fail? – ProgrammingLlama Jul 13 '22 at 02:56
  • Why are you passing a hard coded session cookie? Is it your website? Since you are creating a http client per request, you are forcing the TCP connection to be destroyed and recreated, could the web server be blocking your second connection as a DDOS protection? – Jeremy Lakeman Jul 13 '22 at 03:08
  • @DiplomacyNotWar The API is not blocking the request. I have already used burp to test 100 requests in a row to that endpoint and i've got 100% response – SrMilton Jul 13 '22 at 03:20
  • @JeremyLakeman I have already checked if the api endpoint was somehow blocking my request but everything it's fine. I have already tried to create just one httpclient and reusing it but still the same and Yes, the session is hardcoded because it's never changes, don't worry with that. btw if i open my program multiple times in a row I aways get the first response correctly. – SrMilton Jul 13 '22 at 03:24
  • Using .NET 6 I can't reproduce an issue with the code you have shown. – ProgrammingLlama Jul 13 '22 at 03:27
  • @DiplomacyNotWar Ok, the code is working using the jsonplaceholder.typicode endpoint. It's look like the bug is only happening when I try to getAsync a endpoint wich send a "set-cookie" parameter as response. I'm gonna to create a API endpoint to test and make sure that's is causing the problem. – SrMilton Jul 13 '22 at 03:37
  • I'd suggest using a `CookieContainer` passed to the handler, rather than manually adding a `Set-Cookie` header. You'd also then be able to share the cookie between multiple calls via the `CookieContainer`. I don't know if that will help to solve your issue or not though. – ProgrammingLlama Jul 13 '22 at 03:41
  • Solved. Was missing a Accept header to make getAsync works correctly. Idk how the first time was working without it. – SrMilton Jul 13 '22 at 04:20

1 Answers1

0

Solved. I was missing a Accept header on my getAsync headers.

client2.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
               
SrMilton
  • 45
  • 6