HttpWebRequest登录论坛及其cookies的保存(转)
来源:互联网 发布:手机千里眼监控软件 编辑:程序博客网 时间:2024/05/21 11:30
string LOGIN_URL = @"http://www.pdhr.com/login.aspx";
string SECRET_PAGE_URL = "http://www.pdhr.com/enterprise/EnterpriseToday.aspx?MenuID=7";
HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
StreamReader responseReader = new StreamReader(
webRequest.GetResponse().GetResponseStream()
);
string responseData = responseReader.ReadToEnd();
responseReader.Close();
// extract the viewstate value and build out POST data
string viewState = ExtractViewState(responseData);
string postData =
String.Format(
"__VIEWSTATE={0}&UserLogin1:S_tbloginnamee={1}&UserLogin1:S_tbpassword={2}&UserLogin1:loginradio={3}&UserLogin1:S_btconfirm={4}",
viewState, "敏泰", "123456", "S_rbperson", "确 认"
);
// have a cookie container ready to receive the forms auth cookie
CookieContainer cookies = new CookieContainer();
CookieContainer reqcookies = new CookieContainer();
// now post to the login form
webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
webRequest.CookieContainer = cookies;
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
// we don't need the contents of the response, just the cookie it issues
webRequest.GetResponse().Close();
CookieCollection cookieheader = webRequest.CookieContainer.GetCookies(new Uri(LOGIN_URL));
// now we can send out cookie along with a request for the protected page
webRequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;
webRequest.CookieContainer = cookies;
webRequest.Referer = SECRET_PAGE_URL;
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
webRequest.ContentType = "application/x-www-form-urlencoded";
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream(),Encoding.GetEncoding("gb2312"));
cookieheader = webRequest.CookieContainer.GetCookies(new Uri(SECRET_PAGE_URL));
// and read the response
responseData = responseReader.ReadToEnd();
responseReader.Close();
Response.Write(responseData);
=========================================
关于采用WinForms方式登录Web论坛,已经研究有一段时间了。一波三折,终于有点满意的结论了!
- HttpWebRequest登录论坛及其cookies的保存(转)
- httpwebrequest 模拟登录 获取cookies 以前的代码,记录备忘!
- httpwebrequest 模拟登录 获取cookies 以前的代码,记录备忘!
- 保存 webbrowser 中的 cookies 到 httpwebrequest 中
- 利用cookies 保存登录信息
- 利用cookies 保存登录信息
- 登录时保存用户cookies
- ASP.NET使用Cookies保存登录信息
- java使用cookies保存用户登录信息
- 浏览器保存Cookies的位置
- HttpWebRequest.CookieContainer与HttpWebResponse.Cookies的区别和联系
- c# HttpWebRequest 代码手动设置cookies的简单办法
- HttpWebRequest.CookieContainer与HttpWebResponse.Cookies的区别和联系
- 抓取cookies登录的页面
- ASP.NET 关于session和cookies保存用户登录信息
- c# HttpWebRequest 和HttpWebResponse 登录网站或论坛(校内网登陆)
- cookies的清空和保存
- CCleaner选择要保存的Cookies设置
- 【Java练习1】POJ 1131
- 学习笔记(2)string与stringBulider区别
- Ext.grid.GridPanel的MVC体现
- 【DB2】如何精确定位到死锁
- VS2008找不到MFC90d.dll错误解决方法
- HttpWebRequest登录论坛及其cookies的保存(转)
- xcode4中UIViewController为什么没了dealloc方法
- jquery val() 和 value 的区别
- Java 三扇门
- MySQL创建外键[条件/注意事项]
- 单链表是否有环并如何找到环入口
- MIPS 汇编指令学习指令-abs
- ASP.NET+Extjs2.0读取Json数据显示在GridPanel面板上面
- URAL - 1012 DP+高精度