我怎么能用httpclient来表示登录失败 [英] How can I figure login failed with httpclient

查看:82
本文介绍了我怎么能用httpclient来表示登录失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


i已经尝试解决这个问题,但我不能

i希望用HttpClient登录网站,但我想知道登录失败时

这个是我的代码:



hi i have try to solved this problem but i can't
i want login into website with HttpClient but i want to know when login failed
this is my code :

static void Main(string[] args)
        {
            login(@"http://www.test.com/", "a784512", "112233445").Wait();

            Console.ReadKey();
        }

        private static async Task login(string host, string username, string password)
        {
            var client = new HttpClient();
            client.BaseAddress = new Uri(host);
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("j_username", username),
                new KeyValuePair<string, string>("j_password", password)
            });


            var result = await client.PostAsync("login.html", content);
            var ok = result.IsSuccessStatusCode;
            Console.WriteLine((int)result.StatusCode);
            Console.WriteLine();
            Console.WriteLine(ok.ToString());
            Console.WriteLine(result.Headers.Location);
        }







因为你看到我试图得到一些错误或任何东西......这是我们发送错误用户名或密码的一点,我们获得状态200

仅在正确登录和不良登录之间有所不同是在响应参数中我们收到的错误数据错误:true



所以..我该怎么办?



我尝试了什么:



阅读状态代码 - >不结果

回读参数 - >我找不到这个参数




as you see i tried to get some error or anything ... this is a point that we send wrong username or password we get status 200
only different between right login and bad login is in response parameter that in wrong data we receive error:true

so .. what must i do ?

What I have tried:

read status code -> not result
read back param -> i cant find this param

推荐答案

你可以检查你设置的结果 =等待client.PostAsync( );对于null。



这里是我写的一个项目的链接,用于保存。

n支持台第2天 [ ^ ]
You can just check result that you set = to await client.PostAsync(); for null.

And here is a link to a project that I wrote to do the save thing.
Support Desk Day 2 of n[^]


这篇关于我怎么能用httpclient来表示登录失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆