需求描述:
我是写的一个用程序发出请求,得到响应,登陆后可以得到cookie,通过得到的cookie(session也是利用cookie嘛),可以用程序向下面这样访问登陆后的页面,而现在我要的效果是:当这个aspx页面(这段程序暂时写在aspx.cs里面),发送到客户端浏览器
时,此浏览器也可以访问登陆后操作, 我的思路是,当aspx页面发送到浏览器时,将用程序得到的System.Net.Cookie 写到客户端去,让浏览器再次访问此地址时自动发送cookie 就可以访问登陆后页面了,下面是代码:
string url4 = "http://mis.lohoo.com/mis/reservation/newrev3/addnew1.asp?come_from_hba=hba"; string postData4 = "hotel_name={0}&come_from_hba=hba&card_no=613613285&password=lohoo&arrive_date=2009-4-2&leave_date=2009-4-3&mis_map=yes&isreserve=0&query=query";
postData4 = postData4.Replace("{0}", "北京好意舍酒店");
byte[] byteArray4 = System.Text.Encoding.GetEncoding("GB2312").GetBytes(postData4);
Uri u4 = new Uri(url4);
HttpWebRequest request4 = (HttpWebRequest)WebRequest.Create(u4);
request4.CookieContainer = cc;
request4.Method = "POST";
request4.ContentType = "application/x-www-form-urlencoded";
request4.ContentLength = byteArray4.Length;
request4.AllowAutoRedirect = true;
Stream newStream4 = request4.GetRequestStream();
newStream4.Write(byteArray4, 0, byteArray4.Length); //写入参数
newStream4.Close();
HttpWebResponse webresponse4 = (HttpWebResponse)request4.GetResponse();
foreach (Cookie cookie in cc.GetCookies(request4.RequestUri))
{
HttpCookie cookie1 = new HttpCookie(cookie.Name, cookie.Value);
Response.Cookies.Add(cookie1);
}
StreamReader sr5 = new StreamReader(webresponse4.GetResponseStream(), Encoding.Default);
string htmlCode5 = sr5.ReadToEnd(); string aa = diggerUtils.cutHtml(htmlCode5, "<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\">","</table>");
Literal1.Text = "<table>"+aa+"</table>";涉及到两个方面:一是如何把此程序的到的cookie写到客户端去,上面办法不行.二是 System.Net.Cookie 转 HttpCookie
我是写的一个用程序发出请求,得到响应,登陆后可以得到cookie,通过得到的cookie(session也是利用cookie嘛),可以用程序向下面这样访问登陆后的页面,而现在我要的效果是:当这个aspx页面(这段程序暂时写在aspx.cs里面),发送到客户端浏览器
时,此浏览器也可以访问登陆后操作, 我的思路是,当aspx页面发送到浏览器时,将用程序得到的System.Net.Cookie 写到客户端去,让浏览器再次访问此地址时自动发送cookie 就可以访问登陆后页面了,下面是代码:
string url4 = "http://mis.lohoo.com/mis/reservation/newrev3/addnew1.asp?come_from_hba=hba"; string postData4 = "hotel_name={0}&come_from_hba=hba&card_no=613613285&password=lohoo&arrive_date=2009-4-2&leave_date=2009-4-3&mis_map=yes&isreserve=0&query=query";
postData4 = postData4.Replace("{0}", "北京好意舍酒店");
byte[] byteArray4 = System.Text.Encoding.GetEncoding("GB2312").GetBytes(postData4);
Uri u4 = new Uri(url4);
HttpWebRequest request4 = (HttpWebRequest)WebRequest.Create(u4);
request4.CookieContainer = cc;
request4.Method = "POST";
request4.ContentType = "application/x-www-form-urlencoded";
request4.ContentLength = byteArray4.Length;
request4.AllowAutoRedirect = true;
Stream newStream4 = request4.GetRequestStream();
newStream4.Write(byteArray4, 0, byteArray4.Length); //写入参数
newStream4.Close();
HttpWebResponse webresponse4 = (HttpWebResponse)request4.GetResponse();
foreach (Cookie cookie in cc.GetCookies(request4.RequestUri))
{
HttpCookie cookie1 = new HttpCookie(cookie.Name, cookie.Value);
Response.Cookies.Add(cookie1);
}
StreamReader sr5 = new StreamReader(webresponse4.GetResponseStream(), Encoding.Default);
string htmlCode5 = sr5.ReadToEnd(); string aa = diggerUtils.cutHtml(htmlCode5, "<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\">","</table>");
Literal1.Text = "<table>"+aa+"</table>";涉及到两个方面:一是如何把此程序的到的cookie写到客户端去,上面办法不行.二是 System.Net.Cookie 转 HttpCookie
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货