.net 模拟登录网站请问下各位,利用HttpWebRequest我现在在一个论坛上已经模拟登录上去了!现在的问题是我怎
.net 模拟登录网站
请问下各位,利用HttpWebRequest我现在在一个论坛上已经模拟登录上去了!
现在的问题是我怎么把我模拟登录的这个页面获取到的Cookie传输到这个论坛上,随便点这个论坛上的哪个链接都是显示已经登录上去了的!
[解决办法]
用抓包软件抓取请求响应,再照着实现。
大致是使用cookiecontainer,参考http://wenku.baidu.com/view/4ae4f900b52acfc789ebc921.html
[解决办法]
#1楼的那一行文字跟你说的很具体了,可你就是视而不见。
[解决办法]private CookieContainer cc = new CookieContainer();
private void btnLogin_Click(object sender, EventArgs e)
{
string str = "zjh=********&mm=***";
HttpWebRequest request = WebRequest.Create("http://***:8080/loginAction.do") as HttpWebRequest;
request.Referer = "http://***:8080/logout.do";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cc;
using (Stream stream = request.GetRequestStream())
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
stream.Write(buffer,0,buffer.Length);
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream,Encoding.GetEncoding("gbk"));
string ReturnData = reader.ReadToEnd();
txtContent.Text = ReturnData;
}
///////////////////////////////////////////
HttpWebRequest req = WebRequest.Create("http://***:8080/menu/s_top.jsp") as HttpWebRequest;
//req.Referer = "http://***:8080/loginAction.do";
req.Method = "GET";
req.CookieContainer = cc;
req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
string ReturnData2 = "";
using (Stream stream = res.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gbk"));
ReturnData2 = reader.ReadToEnd();
txtContent.AppendText("\r\n");
txtContent.AppendText("\r\n");
txtContent.AppendText(ReturnData2);
}
if (ReturnData2.Contains("欢迎"))
{
MessageBox.Show("登录成功");
}