Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

C#
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);
        }



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
Posted
Updated 13-Apr-16 10:32am

1 solution

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[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900