HTTPClient POST挂起/超时异常

时间:2015-11-05 16:10:31

标签: c# async-await dotnet-httpclient

在每个人都告诉我增加超时值之前我不想增加它,因为100秒不够长就没有意义。所有其他get和post响应在没有抛出超时/任务取消异常的情况下花费的时间更少。

我试图模仿以下内容,我将用户和密码排除在外...... enter image description here

以下是代码

    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();

继续前进。打印的东西,somethingelse不

  

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

我没有看到任何理由为什么它应该挂起并在这里失败......

摘要:为什么我的帖子没有通过?我是在使用错误的地址还是其他什么?

2 个答案:

答案 0 :(得分:0)

试试这个

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

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

您可以获得有关Httpclient Async call

的更多信息

答案 1 :(得分:0)

由于某种原因,该过程不能是多线程的。

从任务切换到阻止单线程,它工作得很好......

相关问题