如何以编程方式登录网站以截屏? [英] How to programmatically log in to a website to screenscape?

查看:37
本文介绍了如何以编程方式登录网站以截屏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要来自非我网站的一些信息,为了获取这些信息,我需要登录该网站以收集信息,这是通过 HTML 表单进行的.如何在 C# 中进行这种经过身份验证的屏幕截图?

I need some information from a website that's not mine, in order to get this information I need to login to the website to gather the information, this happens through a HTML form. How can I do this authenticated screenscaping in C#?

额外信息:

  • 基于 Cookie 的身份验证.
  • 需要 POST 操作.

推荐答案

您会像刚刚填写表格一样提出请求.假设它是 POST,例如,您使用正确的数据发出 POST 请求.现在,如果您无法直接登录到要抓取的同一页面,则必须跟踪在您的登录请求之后设置的任何 cookie,并将它们包含在您的抓取请求中,以便您保持登录状态.

You'd make the request as though you'd just filled out the form. Assuming it's POST for example, you make a POST request with the correct data. Now if you can't login directly to the same page you want to scrape, you will have to track whatever cookies are set after your login request, and include them in your scraping request to allow you to stay logged in.

它可能看起来像:

HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest;
http.KeepAlive = true;
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";
string postData="FormNameForUserId=" + strUserId + "&FormNameForPassword=" + strPassword;
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;
// Probably want to inspect the http.Headers here first
http = WebRequest.Create(url2) as HttpWebRequest;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(httpResponse.Cookies);
HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;

也许吧.

这篇关于如何以编程方式登录网站以截屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆