2012-04-17 76 views
0

我正尝试在我创建的Google Web应用程序项目中通过Java访问Web服务。无法在Google Web应用程序项目中获取Cookie

代码应该打到端点URL并通过用户名/密码对用户进行身份验证,并获取服务器在cookie中提供的jsessionID。身份验证工作正常,但我无法从cookie中提取JsessionID。

以下是我使用的代码: -

private static String getCookieFromHeaders(HttpURLConnection wsConnection) { 

    String headerName; 
    String headerValue = "FAIL"; 
    for (int i = 0;; i++) { 
     headerName = wsConnection.getHeaderFieldKey(i); 
     if (headerName != null && headerName.equals("Set-Cookie")) { 
      headerValue = wsConnection.getHeaderField(i); 
      break; 
     } 
    } 
    // return the header value (FAIL string for not found) 
    return headerValue; 
} 

变量headerName只是在一个无限循环给“空”。

请指教,这里是一个完整的Java noob。

+0

忘了添加...当我创建我的eclipse项目作为一个普通的Java项目时,此代码工作绝对正常,但目前我需要在一个从JSP页面调用的servlet中运行此cookie。 – Vic 2012-04-17 17:42:23

回答

0

实际上正在接收cookie,标题是“set-cookie”而不是“Set-Cookie”。 因为愚蠢的无限循环而无法发现它!

0

为什么不直接使用HttpServletRequest.getCookies()?

+0

cookie正在通过HttpURLConnection获取,即当我打到Web服务端点时 – Vic 2012-04-18 06:31:51

相关问题