弘帝企业智能建站系统交流平台

 找回密码
 立即注册
查看: 1867|回复: 0

【C#】HttpWebRequest POST传值丢失Session的理解

[复制链接]
发表于 2020-1-11 22:40:06 | 显示全部楼层 |阅读模式
以下的理解可能不正确,是否正确待验证!

1.aspx
  1. protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             // 给会话添加一项
  4.             Session.Add("ss", "ttttt");
  5.             string cookie = "";
  6.             string outcookie = "";
  7.             string s2 = HttpWebResponsePost("http://host/test/2.aspx", "", "utf-8", cookie, out outcookie);
  8.             Response.Write(s2);
  9.         }
复制代码


2.aspx
  1.         protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             Response.Write("request page... Session Count:" + Session.Count);
  4.             Response.Write("<br>Session["ss"]=" + Session["ss"].ToString());
  5.         }
复制代码


3.aspx
  1. protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             //请求3.aspx得到HTML
  4.             string cookie = "";
  5.             string outcookie = "";
  6.             string s1 = HttpWebResponsePost("http://host/test/1.aspx", "", "utf-8", cookie, out outcookie);
  7.             string cookie2 = outcookie;
  8.             string s2 = HttpWebResponsePost("http://host/test/2.aspx", "", "utf-8", cookie2, out outcookie);
  9.             Response.Write(s2);
  10.         }

  11.         public static string HttpWebResponsePost(string url, string postData, string encodeType, string cookie, out string outcookie)
  12.         {

  13.             Stream outstream = null;
  14.             Stream instream = null;
  15.             StreamReader sr = null;
  16.             HttpWebResponse response = null;
  17.             HttpWebRequest request = null;
  18.             Encoding encoding = Encoding.GetEncoding(encodeType);
  19.             byte[] data = encoding.GetBytes(postData);

  20.             try
  21.             {
  22.                 request = WebRequest.Create(url) as HttpWebRequest;
  23.                 CookieContainer cookieContainer = new CookieContainer();
  24.                 request.CookieContainer = cookieContainer;
  25.                 if (cookie != "")
  26.                 {
  27.                     request.CookieContainer.SetCookies(new Uri(url), cookie);
  28.                 }
  29.                 request.AllowAutoRedirect = true;
  30.                 request.Method = "POST";
  31.                 request.ContentType = "application/x-www-form-urlencoded";
  32.                 request.ContentLength = data.Length;
  33.                 outstream = request.GetRequestStream();
  34.                 outstream.Write(data, 0, data.Length);
  35.                 outstream.Close();
  36.                 response = request.GetResponse() as HttpWebResponse;
  37.                 instream = response.GetResponseStream();

  38.                 outcookie = request.CookieContainer.GetCookieHeader(new Uri(url));
  39.                 sr = new StreamReader(instream, encoding);
  40.                 string content = sr.ReadToEnd();
  41.                 HttpContext.Current.Response.Write(HttpContext.Current.Session["ss"]);
  42.                 return content;
  43.             }
  44.             catch (Exception ex)
  45.             {
  46.                 outcookie = "";
  47.                 return string.Empty;
  48.             }
  49.         }
复制代码


1.aspx POST 2.aspx时,不显示Session值,看起来似乎丢了Session。
理解如下:当HTTPWEBREQUEST时POST 2.aspx 时,实现等于执行了一次无Session传值的2.aspx,不会显示Session值。但1.aspx的Session值已经传入了2.aspx。只是Response.Write("<br>Session[\"ss\"]=" + Session["ss"].ToString());不是传值Session后的值,所以不可能显示tttttt值

当3.asp将outcookie传给新cookie2时,实际上2.aspx符合读取Session流的Session存在,故能读取Session[“tt”]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|弘帝企业智能建站系统 ( 皖ICP备07503252号 )

GMT+8, 2024-4-26 06:27 , Processed in 0.094682 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表