public partial class _Default : System.Web.UI.Page
{
CookieContainer m_cc = new CookieContainer();
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string s;
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create("https://passport.baidu.com/?login");
System.Net.ServicePointManager.Expect100Continue = false;
httpReq.Method = "POST";
httpReq.CookieContainer = m_cc;
httpReq.Accept = "*/*";
httpReq.Referer = "http://passport.baidu.com/?login";
httpReq.ContentType = "application/x-www-form-urlencoded";
httpReq.Headers.Add("Accept-Encoding", "gzip, deflate");
httpReq.Headers.Add("Accept-Language", "zh-cn");
httpReq.Headers.Add("Cache-Control", "no-cache");
httpReq.Headers.Add("UA-CPU", "x86");
httpReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2)";
string strPostData = GetPostData();
byte[] bsInData =UTF8Encoding.Default .GetBytes(strPostData);
httpReq.ContentLength = bsInData.Length; Stream inputStream = httpReq.GetRequestStream();
inputStream.Write(bsInData, 0, bsInData.Length);
inputStream.Close();
inputStream.Dispose(); HttpWebResponse httpRes = null;
httpRes = (HttpWebResponse)httpReq.GetResponse();
Stream outStream = httpRes.GetResponseStream();
StreamReader sr = new StreamReader(outStream, System.Text.Encoding.GetEncoding("GB2312")); s=sr.ReadToEnd();
sr.Close();
Response.Write(s);
}
private string GetPostData()
{
string strRtn = ""; NameValueCollection nvc = new NameValueCollection();
nvc.Add("aid", "");
nvc.Add("more_param", "");
nvc.Add("need_coin", "");
nvc.Add("need_pay", "");
nvc.Add("next_target", "");
nvc.Add("password", "*****");
nvc.Add("pay_method", "");
nvc.Add("psp_tt", "0");
nvc.Add("return_method", "get");
nvc.Add("return_type", "");
nvc.Add("safeflg", "0");
nvc.Add("skip_ok", "");
nvc.Add("tpl", "mn");
nvc.Add("tpl_ok", "");
nvc.Add("u", "http://www.baidu.com/");
nvc.Add("username", *****");
nvc.Add("verifycode", "");
foreach (string ie in nvc)
{
strRtn += ie + "=" + nvc[ie] + "&";
}
if (strRtn.EndsWith("&"))
strRtn = strRtn.Substring(0, strRtn.Length - 1);
return strRtn; ;
}
}通过post数据实现登录到baidu 为什么却登录不上呢,该怎么实现Post数据中的用户名和密码已经用星号代替问题出在什么地方那个,已经确认我的用户名和密码没有错误
httpReq.Headers.Add("Cache-Control", "no-cache");
这2个协议头不是这么添加的吧?