2012-08-16 69 views
0

构建与使用JSESSIONID cookie的站点交互的ASP.Net应用程序。 我给出的规范说要返回每个请求的Cookie。 我将cookie从登录响应的头部提取出来,但是当我尝试在以下请求中使用它时,出现'PlatformNotSupported错误' 显然这是由于安全性的“改进”。 我已经看到博客讨论编写代理服务器以及创建从IHttpModule派生的类。 混淆我一号 是否有一个简单的推荐方式编码HttpWebRequest,以便我可以传输Cookie?Asp.Net HTTPWebRequest返回JSESSIONID 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 
+0

Bob您是否有可以发布的代码示例?你试图通过一个网络服务偶然做到这一点..? – MethodMan 2012-08-16 22:05:06

回答

1

找到它了。 事实证明,我应该做了一个饼干收集,并将其分配给我原来的登录请求。
然后,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); 
    }