[C#] 纯文本查看 复制代码 void getLoginedCookie(string url)
{
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.GetEncoding("utf-8");
request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cc = new CookieContainer();
request.CookieContainer = cc;
request.AllowAutoRedirect = true;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送get请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
if (response.Cookies.Count > 1)
{
HttpCookie usernameCookies = new HttpCookie("USER.USERNAME");//新建本地cookie
usernameCookies.Expires = DateTime.Now.AddYears(50);//过期50年
usernameCookies.Domain = "baidu.com";
usernameCookies.Value = response.Cookies["USER.USERNAME"].Value;
HttpCookie authCookies = new HttpCookie("USER.AUTH");
authCookies.Expires = DateTime.Now.AddYears(50);
authCookies.Domain = "baidu.com";
authCookies.Value = response.Cookies["USER.AUTH"].Value;
Response.Cookies.Add(usernameCookies);//将response的cookie重写到本地
Response.Cookies.Add(authCookies);
Response.Write("<script language='javascript'>window.location='http://baidu.com'</script>");
}
else
{
return;
}
} |