代码可以直接贴到winfrom里断点执行,
直接到firebug里跟踪可以看到cookie
http://vip.fang.58.com/login.aspx
string postData="__VIEWSTATE=/wEPDwUKLTcxMDYwMzYwNGRkIr9b9tytd6ITkVMabkIG5x3FrHc=& amp;companyName=&loginSubmit=%E7%99%BB%E5%BD%95&loginname=& loginpwd=&loginSubmit=登录";
//我用get的方式获取不到,索性把账号密码都制空,让它报错,
string url="http://vip.fang.58.com/login.aspx";
HttpWebRequest request;
HttpWebResponse response = null;
string msg = "";
byte[] b = null;
b = Encoding.UTF8.GetBytes(postData);
request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.Timeout = 30000;
request.AllowAutoRedirect = true;
request.ContentType = "text/html; charset=utf-8";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-2.0 Firefox/3.6.13";
request.Accept = "text/html,application/xhtml xml,application/xml;q=0.9,*/*;q=0.8";
//request.Headers.Add("Accept-Language", "zh-CN");
//request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.KeepAlive = false;
request.ContentLength = b.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(b, 0, b.Length);
}
try
{
using (response = request.GetResponse() as HttpWebResponse)
{//这里没有值;
string aaa = request.Headers.Get("Set-Cookie");
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
msg = reader.ReadToEnd();
//这里是0;
if (response.Cookies.Count > 0)
{
cookie.Add(response.Cookies);
}
if (msg.Contains(str))
{
return true;
}
else
{
return false;
}
}
}
}
catch
{
return false;
}
request.ContentType="application/x-www-form-urlencoded";
postData 里的用户名,密码都为空,提交后,服务器直接返回登录失败的页面,自然也就不会 set cookie 了。
可以用webbrowser试试啊,webbrowser可以自动记录Cookie的,并且与IE共享可以。
另外:可以吧request.KeepAlive = false;改成 request.KeepAlive = true;试试
想知道你最后是如何拿到所有的cookie的,我目前只拿的到set-cookie里边的..