string url = "https://www.baidu.com";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string postdata = "phone=111111&password=2222222";
byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(postdata);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = requestBytes.Length;
req.Referer = "https://www.xxx.com";
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
string backstr = sr.ReadToEnd();
//Response.Write(backstr);
sr.Close();
res.Close();
string cookiestr = res.Headers.Get("Set-Cookie");//获取Cookie
//foreach (Cookie coo in res.Cookies)
// Response.Write(coo.Name + "==" + coo.Value + "<br/>");
string aaa = "";
foreach (Cookie coo in res.Cookies)
{
aaa += coo.Value + ",";
}
//显示"登录"
return backstr + "-----------------------------------------------------------------" + aaa;