在C#中使用asp.net表单登录屏幕抓取网站?

bdd*_*bdd 7 c# screen-scraping

是否可以为受表单登录保护的网站编写屏幕抓取器.当然,我可以访问该网站,但我不知道如何登录该网站并将我的凭据保存在C#中.

此外,C#中任何关于屏幕共享的好例子都会非常受欢迎.

这已经完成了吗?

Luk*_*kas 6

这很简单.您需要自定义登录(HttpPost)方法.

你可以想出这样的东西(这样你就可以在登录后获得所有需要的cookie,你只需将它们传递给下一个HttpWebRequest):

public static HttpWebResponse HttpPost(String url, String referer, String userAgent, ref CookieCollection cookies, String postData, out WebHeaderCollection headers, WebProxy proxy)
    {
        try
        {
            HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest;
            http.Proxy = proxy;
            http.AllowAutoRedirect = true;
            http.Method = "POST";
            http.ContentType = "application/x-www-form-urlencoded";
            http.UserAgent = userAgent;
            http.CookieContainer = new CookieContainer();
            http.CookieContainer.Add(cookies);
            http.Referer = referer;
            byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
            http.ContentLength = dataBytes.Length;
            using (Stream postStream = http.GetRequestStream())
            {
                postStream.Write(dataBytes, 0, dataBytes.Length);
            }
            HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
            headers = http.Headers;
            cookies.Add(httpResponse.Cookies);

            return httpResponse;
        }
        catch { }
        headers = null;

        return null;
    }
Run Code Online (Sandbox Code Playgroud)