C#屏幕抓取使用POST处理登录控件

时间:2010-11-08 20:13:00

标签: screen-scraping

我正在尝试使用屏幕抓取登录网站,我遇到了困难。

 private static string GetWebRequestContent(Uri uri)
    {
        string sid = String.Empty;

        string userName = "username";
        string password = "password";
        string postData = string.Format("FormName=Login&Login={0}&Password={1}&FormAction=login&ret_page=%2FDefault.asp&querystring=",userName,password);
        byte[] postBytes = Encoding.UTF8.GetBytes(postData);


        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
        req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.2; .NET4.0C; .NET4.0E; MS-RTC LM 8)";
        req.KeepAlive = false;
        req.Proxy = new WebProxy(IPAddress,PORT);
        req.Method = "POST";
        req.Host = host;
        req.Referer = referer;
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = postBytes.Length;

        StreamWriter requestwriter = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
        requestwriter.Write(postData);
        requestwriter.Close();

        try
        {
            using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader sr = new StreamReader(res.GetResponseStream()))
                {
                    sid = sr.ReadToEnd().Trim();
                }
            }
        }
        catch (WebException e)
        {
            Console.WriteLine(uri.ToString());
            Console.WriteLine(e.Message);
        }

        return sid;
    }

我得到的所有内容都是登录表单,它永远不会让我登录。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试监控浏览器中发送的请求,然后在脚本中复制此请求。有各种firefox扩展程序可以帮助您执行此操作,例如TamperDataFirebugLiveHttp