1
我收到了一個Android應用程序,該應用程序應該與使用Jersey
和Jetty
實施的Web服務進行通信。將會話ID從Android應用發送到Jersey網絡服務
我可以使用此代碼讀取傳入會話ID:
private static void getCookie(HttpResponse httpResponse)
{
Header[] headers = httpResponse.getAllHeaders();
for (int i=0; i < headers.length; i++)
{
Header h = headers[i];
String s1 = h.getName();
if(s1.equals("Set-Cookie"))
{
sessionCookieValue = h.getValue().split(";",2)[0];
return;
}
}
}
這似乎是工作,我在cookie值得到這樣一個值:JSESSIONID=8pdfuwgduykls5cvj971ylrc
的問題是,我不似乎發送正確。該Web服務注入了一個HttpServletRequest
參數,該參數應該保存會話標識,但它沒有。
我試圖將它傳遞是這樣的方式:
DefaultHttpClient client = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", sessionCookieValue);
cookie.setPath("/");
cookie.setDomain(MyApplication.WEB_SERVICE_BASE_ADDRESS); //"192.168.200.158/"
cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
HttpResponse httpResponse;
try
{
httpResponse = client.execute(request);
...
}
爲什麼沒有會話ID注入到Web服務請求?
嘗試設置cookie域。另外,在你的代碼中,cookie的名字是「Cookie」。我認爲這是虛擬的價值,因爲它應該是「JSESSIONID」 –
該域名究竟應該是什麼?我只是連接到一個IP地址的網絡服務我得到了 –
然後設置cookie的域值爲IP地址 –