开发者

How to login from code

I need to download page sources from site "hh.ua", but first I ha开发者_JS百科ve to login. I tried many different methods, but logging failed. Action source of that page is "http://hh.ua/logon.do", I tried to monitor the query sent for logging, it is - http://hh.ua/logon.do?username=111&password=333&action=%D0%92%D0%BE%D0%B9%D1%82%D0%B8. Please, help!)

Here is code

        string formUrl = "http://hh.ua/logon.do"; 
        string formParams = string.Format("username={0}&password={1}&action=%D0%92%D0%BE%D0%B9%D1%82%D0%B8", "anli_@mail.ru", "80903798516");
        string cookieHeader;
        WebRequest req = WebRequest.Create(formUrl);
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(formParams);
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }
        WebResponse resp = req.GetResponse();

        cookieHeader = resp.Headers["Set-cookie"];
        return cookieHeader;

        string pageSource;
        string getUrl = "http://hh.ua/resume/ae23733eff00c000df0039ed1f476831706856";
        WebRequest getRequest = WebRequest.Create(getUrl);
        getRequest.Headers.Add("Cookie", cookieHeader);
        WebResponse getResponse = getRequest.GetResponse();
        using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
        {
            pageSource = sr.ReadToEnd();
        }

I know about failing of logging because downloaded page is login page.


For those projects that involve a lot of web back and forth, I find WatiN very useful. While WatiN may have origins in the test-driven world, it's pretty good at anything that needs to steer a web browser (now with Chrome and Firefox support :).


You can use this technique that I describe here to auto login:
http://codecube.net/2010/02/reusing-phpbbs-authentication-system/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜