如何在没有浏览器的情况下与网站进行互动?

说我正在构建ac#应用程序。 申请目的:

  1. 从用户获取用户名和密码。
  2. 并显示网站上的一些信息。

在后台,在获取用户名和密码后,它应该:

  1. 使用这些凭据登录网站。
  2. 并单击登录后显示的锚链接。
  3. 找出保存信息的范围。
  4. 得到信息。

这是一个例子。 我实际上正在构建一个应用程序来显示带宽使用信息。 服务器不会为此公开任何API。

是否有任何教程/信息/文章可用于类似目的? 我只是不寻找什么?

HttpWebRequests的基本介绍

首先,您将需要适合该工作的工具。 去下载适用于Firefox的Live HTTP Headers插件。 这将允许您实时查看HTTP标头,以便您可以查看与网站交互时发送的POST数据。 一旦您知道发送到网站的数据,您就可以通过编程方式创建自己的HTTP Web请求来模拟该过程。 工具>实时HTTP标头

通过导航到“ 工具”>“实时HTTP标头”来加载实时HTTP标头 。 一旦你加载了GUI导航到你想登录的网站,我将使用Facebook进行演示。 输入您已准备好登录的凭据,但在此之前清除GUI文本窗口并确保选中标记为Capture的复选框。 登录后,您将看到文本窗口中包含有关请求的各种信息,包括您需要的POST数据。

我发现最好单击全部保存…然后在文本文档中搜索您的用户名,以便您可以轻松识别POST数据。 对于我的请求,POST数据看起来像这样:

LSD = AVP-UAbD&显示=&legacy_return = 1&return_session = 0&trynum = 1&charset_test =%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C %D0%94%2C%D0%84&时区= 0&lgnrnd = 214119_mDgc&lgnjs = 1356154880&电子邮件=%myfacebookemail40outlook.com&传= myfacebookpassword&default_persistent = 0

然后可以在C#中定义,如下所示:

 StringBuilder postData = new StringBuilder(); postData.Append("lsd=AVqRGVie&display="); postData.Append("&legacy_return=1"); postData.Append("&return_session=0"); postData.Append("&trynum=1"); postData.Append("&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84"); postData.Append("&timezone=0"); postData.Append("&lgnrnd=153743_eO6D"); postData.Append("&lgnjs=1355614667"); postData.Append(String.Format("&email={0}", "CUSTOM_EMAIL")); postData.Append(String.Format("&pass={0}", "CUSTOM_PASSWORD")); postData.Append("&default_persistent=0"); 

我的目标是向您展示我们可以通过Web浏览器“手动”发送的POST数据与我们如何使用所述数据模拟C#中的请求之间的关系。 了解发送POST数据远非确定性。 不同的网站以不同的方式工作,可以按照你的方式抛出各种各样的东西。 下面是我整理的一个函数,用于validationFacebook凭据是否正确。 我不能也不应该在这里深入研究,因为class级及其成员都有很好的自我记录。 您可以找到比我在MSDN上使用的方法更好的信息,例如WebRequest.Method Property

  private bool ValidateFacebookCredentials(string email, string password) { CookieContainer cookies = new CookieContainer(); HttpWebRequest request = null; HttpWebResponse response = null; string returnData = string.Empty; //Need to retrieve cookies first request = (HttpWebRequest)WebRequest.Create(new Uri("https://www.facebook.com/login.php?login_attempt=1")); request.Method = "GET"; request.CookieContainer = cookies; response = (HttpWebResponse)request.GetResponse(); //Set up the request request = (HttpWebRequest)WebRequest.Create(new Uri("https://www.facebook.com/login.php?login_attempt=1")); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13"; request.Referer = "https://www.facebook.com/login.php?login_attempt=1"; request.AllowAutoRedirect = true; request.KeepAlive = true; request.CookieContainer = cookies; //Format the POST data StringBuilder postData = new StringBuilder(); postData.Append("lsd=AVqRGVie&display="); postData.Append("&legacy_return=1"); postData.Append("&return_session=0"); postData.Append("&trynum=1"); postData.Append("&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84"); postData.Append("&timezone=0"); postData.Append("&lgnrnd=153743_eO6D"); postData.Append("&lgnjs=1355614667"); postData.Append(String.Format("&email={0}", email)); postData.Append(String.Format("&pass={0}", password)); postData.Append("&default_persistent=0"); //write the POST data to the stream using(StreamWriter writer = new StreamWriter(request.GetRequestStream())) writer.Write(postData.ToString()); response = (HttpWebResponse)request.GetResponse(); //Read the web page (HTML) that we retrieve after sending the request using (StreamReader reader = new StreamReader(response.GetResponseStream())) returnData = reader.ReadToEnd(); return !returnData.Contains("Please re-enter your password"); } 

任何HTTP客户端实现,都有大量的开源库。 以curl为例。 有些家伙为它制作了一个.NET包装器 。

您可以继续使用WebClient进行POST(而不是GET,这是您当前使用的DownloadString的HTTP动词),但我认为您会发现使用(稍微)较低级别的WebRequest和WebResponse类更容易。

这有两个部分 – 第一部分是发布登录表单,第二部分是恢复“Set-cookie”标题,并将其作为“Cookie”发送回服务器以及您的GET请求。 从现在开始,服务器将使用此cookie来识别您(假设它使用基于cookie的身份validation,我非常有信心,因为该页面返回包含“PHPSESSID”的Set-cookie标头)。

点击这里查看详细信息

抓取内容示例代码(屏幕抓取)

  Uri uri = new Uri("http://www.microsoft.com/default.aspx"); if(uri.Scheme = Uri.UriSchemeHttp) { HttpWebRequest request = HttpWebRequest.Create(uri); request.Method = WebRequestMethods.Http.Get; HttpWebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string tmp = reader.ReadToEnd(); response.Close(); Response.Write(tmp); } 

有关如何使用HttpWebRequest将数据发布到远程网页的示例代码

  Uri uri = new Uri("http://www.amazon.com/exec/obidos/search-handle-form/102-5194535-6807312"); string data = "field-keywords=ASP.NET 2.0"; if (uri.Scheme == Uri.UriSchemeHttp) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.Method = WebRequestMethods.Http.Post; request.ContentLength = data.Length; request.ContentType = "application/x-www-form-urlencoded"; StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(data); writer.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string tmp = reader.ReadToEnd(); response.Close(); Response.Write(tmp); } 

资源