我需要一些信息从一个网站,不是我的,为了得到这个信息,我需要登录网站,以收集的信息,发生这种情况通过HTML形式。我怎样才能做到这一认证screenscaping在C#?

额外信息:

  • Cookie基于身份验证。
  • 后行动需要。
有帮助吗?

解决方案

你会的要求,因为虽然你只是填写的表格。假设这是员额的例子,你使一个职位的要求与正确的数据。现在如果你不能直接登录到同一页你要刮去,你会有跟踪任何饼干的设置以后的登录的请求,并包括他们在你的刮请求,允许您留记录。

它可能看起来像:

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;

也许。

其他提示

你可以使用 网页浏览器 控制。只给它的网站的URL,然后使用DOM设置的用户名和密码进入正确的领域,并最终发送一个击到提交按钮。这样你什么都不在乎但这两个输入领域和提交按钮。没有cookie处理,没有原HTML分析,没有HTTP嗅-所有的是通过浏览器的控制。

如果你走那样,一些更多的建议:

  1. 你可以防止的控制从装载添加项如闪-能为你节省一些时间。
  2. 一旦你登录,可以获得的任何你需要的信息,从DOM-不需要分析原HTML。
  3. 如果你想的工具更加便携式的情况下网站的变化,在未来,您可以替换您的明确DOM处理与注射JavaScript。JS可以从外部获得的资源,并且一旦把它称为可以执行人口领域和提交。

对于一些情况下, httpResponse.Cookies 将空白。使用 CookieContainer 代替。

CookieContainer cc = new CookieContainer();

HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest;
http.KeepAlive = true;
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";

http.CookieContainer = cc;

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 = cc;

HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;

作为一个外dlambin答案 这是必要的

http.AllowAutoRedirect=false;

否则

HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;

它会让另一个请求的初步网址和你不能检索url2。

你需要使用HTTPWebRequest和做一个职位。这个链接,应该帮助你开始。关键的是,你需要看看HTML形式的页你试图以后来见所有参数的形式需求,以便提交的员额。

http://www.netomatix.com/httppostdata.aspx

http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top