2012-08-16 64 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); 
    }