Asp.Net HTTPWebRequest返回JSESSIONID Cookie

时间:2012-08-16 20:25:26

标签: asp.net httpwebrequest

构建与使用JSESSIONID cookie的站点交互的ASP.Net应用程序。 我给出的规范说每次请求都返回Cookie。 我从登录响应的标题中提取cookie,但是当我尝试在以下请求中使用它时,我得到一个'PlatformNotSupported错误' 显然这是由于安全性的“改善”。 我见过博客讨论编写代理服务器以及创建从IHttpModule派生的类。 迷茫的我。 是否有一种直接推荐的编码HttpWebRequest的方法,以便我可以传输Cookie?

StackOverflow的不常用户,试图发表评论,失败,所以编辑帖子。 代码是:   string cookie =“JSESSIONID =”+ Session [“SessionId”]。ToString();

        if(Request.Cookies["JSESSIONID"]==null)
        {

            //Request.Headers.Add("Cookie",cookie);//PlatformNotSupportedError
            HttpCookie cook = new HttpCookie("JSESSIONID", Session["SessionId"].ToString());
            Request.Cookies.Add(cook);


        } //The prev login response had the cookie in the header not in the cookies collection so I was trying to send back the same way. ASP.net app at this stage. Will be service after get it going. Thx Bob

1 个答案:

答案 0 :(得分:1)

找到它。 事实证明我应该制作一个cookie集合并将其分配给我原来的登录请求 然后,SessionId cookie会在返回时出现在集合中。

byte[] bytes = Encoding.UTF8.GetBytes(xml);
  CookieContainer cookies = new CookieContainer();
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  request.CookieContainer = cookies;
  request.Credentials = new NetworkCredential(user, password);
  request.Method = "POST";
  request.ContentLength = bytes.Length;
  request.ContentType = "text/xml";
  using (Stream requestStream = request.GetRequestStream())
  {
    requestStream.Write(bytes, 0, bytes.Length);
  }