我可以将HtmlAgilityPack委托给您!
我想您尝试访问使用 cookie 来确定用户是否登录(或未登录)的网站。如果没有,它将强制您注册/登录,否则您将无权看到任何内容。我对吗?
您的浏览器存储该 cookie,而您的 C# 没有!(广义而言)
您需要创建一个 cookie 容器来解决该问题。
您的 C#-App 可能会登录,请求 cookie/会话,可能会从响应头中获取 Cookie,然后您应该能够抓取配置文件或任何您想要的东西。
获取发送到服务器的 Post Data。您可以使用Fiddler、Tamper 等工具/插件。
例如 PostdataString: user_name=TESTUSER&password=TESTPASSWORD&language=en&action%3Asubmit=Submit
这是您可以使用的片段。
//Create the PostData
string strPostData = "user_name=" + txtUser.Text + "&password=" + txtPass.Text + "&language=en&action%3Asubmit=Submit";
CookieContainer tempCookies = new CookieContainer();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(strPostData);
//Create the Cookie
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.website.com/login.php");
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.Accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = "http://www.website.com/login.php";
request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1";
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
string sRequestHeaderBuffer = Convert.ToString(response.Headers);
requestStream.Close();
//Stream(-output) of the new website
StreamReader postReqReader = new StreamReader(response.GetResponseStream());
//RichTextBox to see the new source.
richTextBox1.Text = postReqReader.ReadToEnd();
您将需要调整其间的 Cookie 参数并将您当前的 sessionid 以及添加到代码中。这取决于您访问的请求网站。
例如:
request.Headers.Add("Cookie", "language=en_US.UTF-8; StationID=" + sStationID + "; SessionID=" + sSessionID);