Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:38:16+00:00 2026-06-18T09:38:16+00:00

I have a method to POST some data to server. Server may return aswer

  • 0

I have a method to POST some data to server. Server may return aswer means I need change URL and send data to other server. I use for it HttpClient class from 4.5 framework. I use either do-while loop to repeat requests until I do not need redirecton. But there is a problem.

The question is why if I create HttpClient instance outside loop, second await didn’t happen and my programm get out of loop, but if I create HttpClient instance inside the loop again and again – all is fine? Can I reuse one HttpClient for several POST requsts inside do-while loop inside async method?

My working code sample:

public async Task<bool> GameLogin()
{
    JToken r;
    do
    {
        var clientHandler = new HttpClientHandler
        {
            CookieContainer = this.myCContainer,
            AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
        };
        var client = new HttpClient(clientHandler);
        client.DefaultRequestHeaders.Add("client-ver", Versoin);
        client.DefaultRequestHeaders.Add("method", "SignIn");
        client.DefaultRequestHeaders.Add("authKey", AppParams["auth_key"].ToString());

        var content = new StringContent(this.SendStr);

        var answer = await client.PostAsync(this.CurrentUrl, content);
        var rawString = await answer.Content.ReadAsStringAsync();
        DinamicData = JObject.Parse(rawString);
        r = DinamicData["r"];
        if (r == null) continue;
        this.CurrentUrl = string.Format("http://{0}/main.ashx", r);
    } while (r != null);

    return DinamicData.Type != JTokenType.Null;
}

My did not working code sample:

public async Task<bool> GameLogin()
{
    var clientHandler = new HttpClientHandler
    {
        CookieContainer = this.myCContainer,
        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
    };
    var client = new HttpClient(clientHandler);
    client.DefaultRequestHeaders.Add("client-ver", Versoin);
    client.DefaultRequestHeaders.Add("method", "SignIn");
    client.DefaultRequestHeaders.Add("authKey", AppParams["auth_key"].ToString());

    var content = new StringContent(this.SendStr);

    JToken r;
    do
    {
        var answer = await client.PostAsync(this.CurrentUrl, content);
        var rawString = await answer.Content.ReadAsStringAsync();
        DinamicData = JObject.Parse(rawString);
        r = DinamicData["r"];
        if (r == null) continue;
        this.CurrentUrl = string.Format("http://{0}/main.ashx", r);
    } while (r != null);

    return DinamicData.Type != JTokenType.Null;
}

And my second code did not wait await in secont loop turn.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:38:17+00:00Added an answer on June 18, 2026 at 9:38 am

    Okay, now we’ve got the exception…

    I suspect the problem isn’t reusing the HttpClient – it’s reusing this:

    var content = new StringContent(this.SendStr);
    

    I suspect that content is probably being disposed by the HttpClient‘s first call, which makes the second call fail.

    Just move that line inside the loop (it’s inside the loop in your working code), and I expect all will be well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have next form and some ashx <form action=FileUpload.ashx method=POST enctype=multipart/form-data id=frmUpload> <input id=fileupload
I have a AJAX post method that gets xml data from server. But when
I need to send some data from my Android device to my server. I
OK, I have a client doing a POST to a server with some data.
I have been given some generated code which looks like so: <form id=form1 method=post
I have a html form with the data by this post method 'form id='form1'
I am posting some JSON data to my action method like so: $.ajax({ url:
I have the following code which is giving me a Method POST, Status (canceled)
I have made a form validation using ajax post method. But i got the
I have created a Webpage which will post as post method..not as get method.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.