在firfox中正常网页登录的话,用fiddler发现有三个请求,第一个是post,第二三个是给,服务器分别返回三个值200,302,302。我在winform中模拟的也是三个依次为post get get,但是返回值都是302。 想问下这样是不是post请求出了问题?
模拟请求的话,应该模拟哪些数据那?如何判断登陆成功与否?
大家需要的话,我再贴代码
模拟请求的话,应该模拟哪些数据那?如何判断登陆成功与否?
大家需要的话,我再贴代码
HttpWebRequest request = null;
HttpWebResponse response = null;
string gethost = string.Empty;
CookieContainer cc = new CookieContainer();
string Cookiesstr = string.Empty;
try
{
string UserName = "username";
string PassWord = "123456";
string HostUrl = "http%3A%2F%2Fwww.renren.com%2Fhome";
//第一次POST请求
string postdata = "email=" + UserName.Replace("@", "%40") + "&password=" + PassWord + "&icode=" + "&origURL=" + HostUrl + "&domain=renren.com" + "&key_id=&_rtk=2679ea7f";//模拟请求数据,数据样式可以用FireBug插件得到。人人网POST数据时,用户名邮箱中的“@”变为“%40”,所以我们也要作此变化
//string LoginUrl = "http://www.renren.com/PLogin.do";
string LoginUrl = "http://www.renren.com/PLogin.do";
request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
request.Method = "POST";//数据提交方式为POST
//模拟头
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentType = "application/x-www-form-urlencoded";
byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = postdatabytes.Length;
//request.Referer = "http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=" + HostUrl;
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.KeepAlive = true;
//提交请求
Stream stream;
stream = request.GetRequestStream();
stream.Write(postdatabytes, 0, postdatabytes.Length);
stream.Close();
//接收响应
response = (HttpWebResponse)request.GetResponse();
//保存返回cookie
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies;
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
Cookiesstr = strcrook;
//取第一次GET跳转地址
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = sr.ReadToEnd();
response.Close();
string[] substr = content.Split(new char[] { '"' });
gethost = substr[1];
}
catch (Exception)
{
textBox2.Text = "第一次POST出错";
} try
{
request = (HttpWebRequest)WebRequest.Create(gethost);
request.Method = "GET";
request.KeepAlive = true;
request.Headers.Add("Cookie:" + Cookiesstr);
request.CookieContainer = cc;
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse();
//设置cookie
Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
//取再次跳转链接
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string ss = sr.ReadToEnd();
string[] substr = ss.Split(new char[] { '"' });
gethost = substr[1];
request.Abort();
sr.Close();
response.Close();
textBox3.Text = gethost;
}
catch (Exception)
{
textBox3.Text = "第一次GET出错";
}
try
{
//第二次GET请求
request = (HttpWebRequest)WebRequest.Create(gethost); request.Method = "GET";
request.KeepAlive = true;
request.Headers.Add("Cookie:" + Cookiesstr);
request.CookieContainer = cc;
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse(); //设置cookie
Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
//取再次跳转链接
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
//设置cookie
Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
request.Abort();
response.Close();
HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
//Response.Write(content);
textBox1.Text = content;
}
catch (Exception)
{
//textBox1.Text="zui hou chu cu le";第二次GET出错
} }