1

I'm trying to login a website for parsing purpose. while program runs it seems to login to web site. I can see that from fiddler. But at the end of the program, even the status code seems tobe 200, ı find myself not logged in.

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

Any attempt to use cookies failed. I tried this;

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

and I tried this;

        var handler2 = new HttpClientHandler { CookieContainer = cc };
        var request2 = new HttpRequestMessage(HttpMethod.Get, loginUri);
        var response2 = await client.SendAsync(request2);
  • Im not really sure i understand what your problem is. You see the HTTP request going through ok, and then you attempt to do something else but dont seem to be logged in? – Yuval Itzchakov Sep 23 '14 at 19:37
  • possible duplicate of [How do I log into a site with WebClient?](http://stackoverflow.com/questions/4740752/how-do-i-log-into-a-site-with-webclient) – bdimag Sep 23 '14 at 19:37
  • 3
    This is your code to log in. It is not the code to use the site after login. _That's_ the code that's failing. Post a simple example of it. And what do you mean, "any attempt to us cookies failed"? – John Saunders Sep 23 '14 at 19:43
  • Yes that is exactly what happens. when I try to read some data after login to website, ı just can't. – Serkan Eryiğit Sep 23 '14 at 23:13
  • It sounds like you're doing some web scraping ... I'd recommend having a look at something like [Selenium WebDriver](http://docs.seleniumhq.org/docs/03_webdriver.jsp) instead of trying to reinvent the wheel. When you get to complex sites which include javascript API calls, simple methods like this will fail. – dodgy_coder Sep 24 '14 at 00:41
  • Thanks dodgy_coder, I will give it a shot. That is what I was thinking. – Serkan Eryiğit Sep 24 '14 at 02:09
  • it finally worked with selenium. thanks @dodgy_coder . – Serkan Eryiğit Sep 24 '14 at 16:16

0 Answers0