Ya Wang
Ya Wang

Reputation: 1808

HTTPClient POST Hangs/Timeout Exception

Before everyone tells me to increase the timeout values I don't want to increase it because it doesn't make sense that 100 seconds isn't long enough. All other get and post responses take less time without throwing a timeout/task cancel exception.

I am trying to mimic the following, I whited out the user and password... enter image description here

Here is the code

    var baseAddress = new Uri("http://www.foodservice.com/home/fs_login.cfm?logmode=Home");
    var cookieContainer = new CookieContainer();
    using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
    using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
    {
        var trypage = await client.GetAsync("http://www.foodservice.com/home/fs_login.cfm?logmode=Home");
        trypage.EnsureSuccessStatusCode();
 Console.WriteLine("something");
        var content = new FormUrlEncodedContent(new[]
       {
             new KeyValuePair<string, string>("user_name", "someuser"),
             new KeyValuePair<string, string>("password", "somepass"),
             new KeyValuePair<string, string>("logmode", "Home")
         });
        var result = await client.PostAsync("http://www.foodservice.com/home/fs_login_check.cfm", content);
 Console.WriteLine("somethingelse");
        result.EnsureSuccessStatusCode();

It hands on. something prints, somethingelse doesn't

var result = await client.PostAsync("http://www.foodservice.com/home/fs_login_check.cfm", content);

I don't see any reason why it should hang and fail here...

Summary: Why isn't my post getting through? I am I using the wrong address or something?

Upvotes: 0

Views: 3196

Answers (2)

Ya Wang
Ya Wang

Reputation: 1808

For some reason the process could not be multithreaded.

Switched from Tasks to blocking single threaded it worked fine...

Upvotes: 0

Aizaz Ahmed
Aizaz Ahmed

Reputation: 210

Try this

await client.GetAsync("http://www.foodservice.com/home/fs_login.cfm?logmode=Home").ConfigureAwait(false);

and

await client.PostAsync("http://www.foodservice.com/home/fs_login_check.cfm", content).ConfigureAwait(false);

you can get more information on Httpclient Async call

Upvotes: 1

Related Questions