0

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?

Ya Wang
  • 1,758
  • 1
  • 19
  • 41
  • I assume that the process works, and does not time out, if you use a regular web browser to navigate the pages in question? Hard to tell why it does not work, perhaps the site in question has additional checks to verify that the login is posted by a human and not by for example a brute force password cracker. Or perhaps some required cookie is missing from your request? – Culme Nov 05 '15 at 16:28
  • @Culme Ok I dumped the cookies and I am missing a few that should be there when I loaded the page... is there a way to make httpclient wait for all cookies? – Ya Wang Nov 05 '15 at 17:22
  • @Culme I checked most of the javascript and I cannot find where the cookies from .foodservice.com are coming from I only have the www.foodservice.com cookies – Ya Wang Nov 05 '15 at 17:31
  • It is VERY hard to guess why it fails if you have no insight in how the server app works. All you can do is try to mimic the working call in any way possible. OR contact the server app owner and ask them how to make a successful login call. – Culme Nov 05 '15 at 20:05

2 Answers2

1

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

Community
  • 1
  • 1
Aizaz Ahmed
  • 210
  • 1
  • 6
0

For some reason the process could not be multithreaded.

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

Ya Wang
  • 1,758
  • 1
  • 19
  • 41