首先我想知道你有没有一个合法的用户名和密码来访问这个网站,如果没有,就不用往下看了,hacker的东西我懂得不多。如果你有用户名和密码但是不知道怎么用程序提交身份验证信息,往下看。思路: 1、搞清楚网站用的是什么身份验证方式,如果是IIS,就有:a) Basic b) Digest c) Integrated Windows Authentication (NTLM or Kerberos)。 2、参考下面的代码: string website = "http://localhost"; string downloadUrl = website + "/Test"; // Create the WebRequest WebRequest request = HttpWebRequest.Create(downloadUrl); CredentialCache cc = new CredentialCache(); // The supported values for authType are "NTLM", "Digest", "Kerberos", and "Negotiate". cc.Add(new Uri(website), "NTLM", new NetworkCredential("Username", "Password", "Domain")); // The Credentials must be specified for the request to pass the server authentication // You can use your currently logged-in user credential by the following code: // request.Credentials = CredentialCache.DefaultNetworkCredentials; request.Credentials = cc;
// Get the response stream and read the stream System.IO.StreamReader sr = new System.IO.StreamReader(request.GetResponse().GetResponseStream()); Console.WriteLine(sr.ReadToEnd()); sr.Close();
string website = "http://localhost"; string downloadUrl = website + "/Test"; // Create the WebRequest WebRequest request = HttpWebRequest.Create(downloadUrl); CredentialCache cc = new CredentialCache(); // The supported values for authType are "NTLM", "Digest", "Kerberos", and "Negotiate". cc.Add(new Uri(website), "NTLM", new NetworkCredential("Username", "Password", "Domain")); // The Credentials must be specified for the request to pass the server authentication // You can use your currently logged-in user credential by the following code: // request.Credentials = CredentialCache.DefaultNetworkCredentials; request.Credentials = cc;
// Get the response stream and read the stream System.IO.StreamReader sr = new System.IO.StreamReader(request.GetResponse().GetResponseStream()); Console.WriteLine(sr.ReadToEnd()); sr.Close();这个方法就行了
用HttpWebRequest來收發包 登录与抓取时使用相同的CookieContainer就行了
1、发送HttpRequest请求。
2、接收HttpResponse返回的结果。得到特定页面的html源文件。
3、取出包含数据的那一部分源码。
4、根据html源码生成HtmlDocument,循环取出数据。
5、写入数据库。
1、搞清楚网站用的是什么身份验证方式,如果是IIS,就有:a) Basic b) Digest c) Integrated Windows Authentication (NTLM or Kerberos)。
2、参考下面的代码: string website = "http://localhost";
string downloadUrl = website + "/Test"; // Create the WebRequest
WebRequest request = HttpWebRequest.Create(downloadUrl); CredentialCache cc = new CredentialCache(); // The supported values for authType are "NTLM", "Digest", "Kerberos", and "Negotiate".
cc.Add(new Uri(website), "NTLM", new NetworkCredential("Username", "Password", "Domain")); // The Credentials must be specified for the request to pass the server authentication
// You can use your currently logged-in user credential by the following code:
// request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Credentials = cc;
// Get the response stream and read the stream
System.IO.StreamReader sr = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
Console.WriteLine(sr.ReadToEnd());
sr.Close();
用HttpWebRequest來收發包 登录与抓取时使用相同的CookieContainer就行了
,这种方法最好用,,
用HttpWebRequest來收發包
登录与抓取时使用相同的CookieContainer”
string website = "http://localhost";
string downloadUrl = website + "/Test"; // Create the WebRequest
WebRequest request = HttpWebRequest.Create(downloadUrl); CredentialCache cc = new CredentialCache(); // The supported values for authType are "NTLM", "Digest", "Kerberos", and "Negotiate".
cc.Add(new Uri(website), "NTLM", new NetworkCredential("Username", "Password", "Domain")); // The Credentials must be specified for the request to pass the server authentication
// You can use your currently logged-in user credential by the following code:
// request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Credentials = cc;
// Get the response stream and read the stream
System.IO.StreamReader sr = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
Console.WriteLine(sr.ReadToEnd());
sr.Close();这个方法就行了
假如我抓的是sina网站,参数是不是应该这样:
website ="www.sina.com";
Domain应该换成sina