【问题标题】:Asp.Net HTTPWebRequest Returning JSESSIONID CookieAsp.Net HTTPWebRequest 返回 JSESSIONID Cookie
【发布时间】:2012-11-14 21:43:30
【问题描述】:

构建与使用 JSESSIONID cookie 的站点交互的 ASP.Net 应用程序。 我得到的规范说要在每个请求中返回 Cookie。 我从登录响应的标头中提取 cookie OK,但是当我尝试在以下请求中使用它时,出现“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

【问题讨论】:

  • Bob 您有可以发布的代码示例吗?您是否偶然尝试通过网络服务进行此操作..?

标签: asp.net httpwebrequest


【解决方案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);
  }

【讨论】:

    猜你喜欢
    • 2013-06-13
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    相关资源
    最近更新 更多