HttpWebRequest - ASP .NET MVC 3 传递会话状态
我需要获取控制器/操作的 HTML 标记才能生成 PDF。我所做的是:
public ActionResult Index()
{
Session["Message"] = "SESSION-MESSAGE";
String URL = "http://localhost:7401/Home/SuperComplex";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
req.CookieContainer = new CookieContainer();
for (int i = 0; i <= this.Request.Cookies.Count - 1; i++)
req.CookieContainer.Add(
new System.Net.Cookie(
name: this.Request.Cookies[i].Name,
value: Request.Cookies[i].Value,
path: Request.Cookies[i].Path, domain: this.HttpContext.Request.Url.Host)
);
using (var r = req.GetResponse())
{
using (var s = new StreamReader(r.GetResponseStream()))
{
var htmlToPrint = s.ReadToEnd();
Response.Write("<h1>" + htmlToPrint + "</h1>");
}
}
return View();
}
考虑到上述情况,在 SuperComplex 会话中,我应该有 Session["Message"]。但由于某种奇怪的原因,它没有去那里。
我已经检查了 Session.SessionId - 在这两种情况下它是相同的。
另外,在第二次或第三次请求时,请求超时!
再次: http://localhost:7401/(S(SESSION_ID))/Home/About
如果在其他浏览器中请求:会话劫持确实发生 - 但 WebRequest 死了! :(
帮忙 - 有人吗?
I need to acquire HTML markup of a controller/action in order to generate PDF. What I have done is:
public ActionResult Index()
{
Session["Message"] = "SESSION-MESSAGE";
String URL = "http://localhost:7401/Home/SuperComplex";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
req.CookieContainer = new CookieContainer();
for (int i = 0; i <= this.Request.Cookies.Count - 1; i++)
req.CookieContainer.Add(
new System.Net.Cookie(
name: this.Request.Cookies[i].Name,
value: Request.Cookies[i].Value,
path: Request.Cookies[i].Path, domain: this.HttpContext.Request.Url.Host)
);
using (var r = req.GetResponse())
{
using (var s = new StreamReader(r.GetResponseStream()))
{
var htmlToPrint = s.ReadToEnd();
Response.Write("<h1>" + htmlToPrint + "</h1>");
}
}
return View();
}
Considering above said situation, in SuperComplex session, I should have the Session["Message"]. But for some strange reason, it does not go there.
I have checked Session.SessionId - in both cases it is same.
Also, on second or third request, request timesout!
Again: http://localhost:7401/(S(SESSION_ID))/Home/About
If requested in other browser: session hijack does happen - but WebRequest dies! :(
Help - anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 HTML 存储在部分视图中,然后使用 Helper 函数将其解析为字符串。
Store your HTML in a Partial View and then use a Helper function to parse it into a string.