通过c#登录网站失败

时间:2014-09-23 19:29:28

标签: c# authentication dotnet-httpclient scraper

我正在尝试登录网站进行解析。程序运行时似乎登录到网站。我可以从小提琴手那里看到。但在程序结束时,即使状态代码似乎是200,ı发现自己没有登录。

string loginUri = "https://www.sample.com";
string username = "username";
string password = "password";
CookieContainer cc = new CookieContainer();
var handler = new HttpClientHandler { CookieContainer = cc };
var request = new HttpRequestMessage(HttpMethod.Post, loginUri);
handler.AllowAutoRedirect = true;
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
                                            {
                                            { "return_url", "emagaza.php"},
                                            { "user_login", "sampleuser"},
                                            { "password", "sapmplepass"},
                                            { "dispatch[auth.login]", "Giriş yap"}
                                            });
var client = new HttpClient(handler);


var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();

任何使用Cookie的尝试都失败了。 我试过了;

         httpWebRequest2.CookieContainer = cc;
        HttpWebResponse response3 = (HttpWebResponse)httpWebRequest2.GetResponse();

我尝试了这个;

        var handler2 = new HttpClientHandler { CookieContainer = cc };
        var request2 = new HttpRequestMessage(HttpMethod.Get, loginUri);
        var response2 = await client.SendAsync(request2);

0 个答案:

没有答案