2016-06-16 40 views
1

我正在開發一個使用Unity引擎的遊戲,它必須從客戶端C#發送cookie到服務器端--Java,我面臨這個問題(也許是跨平臺問題?我不確定)在java中cookieContainer中檢索數據

我寫在客戶端一堆這樣的代碼

private HttpWebRequest request(){ 
    try{ 
     string url = "http://localhost:8080/..."; 
     var request = (HttpWebRequest)WebRequest.Create(url); 
     request.Timeout = 15000; 
     request.KeepAlive = true ; 
     request.Method= "GET"; 

     CookieContainer cookieContainer = new CookieContainer(); 
     Cookie Authentication = new Cookie("Session" , "09iubasd"); 
     Authentication.Domain = url; 
     cookieContainer.Add(Authentication); 
     request.CookieContainer = cookieContainer; 
     request.Headers.Add("testting", "hascome"); 
     return request; 
    }catch(System.Exception ex){ 
     Debug.Log("[Exception]" + ex); 
     throw ex; 
    } 

} 

和服務器端是用Java編寫春季。我無法在服務器端檢索CookieContainer內的Cookie數據。任何人都可以給我任何建議或任何解決方案來解決這個問題?或者類似於Java中的CookieContainer。我已經Google搜索,但似乎沒有辦法,如果這是一個愚蠢的問題,那麼請教我。非常感謝。 Vince

回答

1

我只是找出原因,我的cookie域設置錯誤的方法。

這裏是我剛剛修復的新測試代碼。希望今後有同樣問題的人幫忙(因爲如果沒人面對這個愚蠢的問題,一定很好)

private HttpWebRequest request(){ 
    try{ 
     System.Uri uri = new System.Uri("http://localhost:8080/..."); 
     var request = (HttpWebRequest)WebRequest.Create(uri); 
     request.Timeout = 15000; 
     request.KeepAlive = true ; 
     request.Method= "GET"; 

     Cookie Authentication = new Cookie("Session" , "09iubasd"); 
     Authentication.Domain = uri.Host; 
     request.CookieContainer = new CookieContainer(); 
     request.CookieContainer.Add(Authentication); 
     request.Headers.Add("testting", "hascome"); 
     return request; 
    }catch(System.Exception ex){ 
     Debug.Log("[Exception]" + ex); 
     throw ex; 
    } 

}