首先打开网站,查看源文件,找到他的登录表单部分。
比如:
用户名: | maxlength="40" size="23" name="username" id="username" /> |
密 码: | maxlength="40" size="23" name="passwd" type="password" id="passwd" /> |
using System;
using System.Data;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

///
/// 登录网站并获取Cookies
///
///
public static CookieContainer Get_Login()
{
CookieContainer cc = new CookieContainer();
string FormURL="http://blog.hnce.net/loginMain.jsp"; //处理表单的绝对URL地址
string FormData = "username=slick&passwd=hedy12345"; //表单需要提交的参数,注意改为你已注册的信息。
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(FormData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
request.Method = "POST"; //数据提交方式
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
//模拟一个UserAgent
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string WebContent = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
return cc;
}
调用以上的方法来获取需要登录才能查看的内容。
CookieContainer cc = new CookieContainer();
cc = Login.Get_Login(); //获取登录Cookies
string PhotoClassURL = "http://blog.hnce.net/xxx.jsp";
HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
Myrequest.CookieContainer = cc;
HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
cc.Add(Myresponse.Cookies);
Stream Mystream = Myresponse.GetResponseStream();
string sHtml = new StreamReader(Mystream, System.Text.Encoding.Default).ReadToEnd();
sHtml即为你登录之后看到的内容。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-541445/ ,如需转载,请注明出处,否则将追究法律责任。