我正在尝试使用HTTPURLConnection类打开与JSP的连接并从servlet接收响应。在JSP中设置了响应头需要在servlet中读取。
以下是代码示例
String strURL = "http://<host>:<port>/<context>/mypage.jsp";
String sCookies = getCookie();//method to get the authentication cookie(**SSOAUTH**) and its value for the current logged in user
URL url = new URL(strURL);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestProperty("Cookie", URLEncoder.encode(sCookies, "UTF-8"));
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.writeBytes("lang=en");
out.flush();
out.close();
//reading the response received from JSP and retrieve header value
response.write(urlConnection.getHeaderField("commAuth") + "<br />");
问题在于传递的SSOAUTH cookie没有发送到JSP页面。如果我发送UID/PWD而不是cookie,就会成功通过身份验证并正确发送响应。
urlConnection.setRequestProperty("username", "testuser");
urlConnection.setRequestProperty("password", "testpwd");
这是通过HTTPURLConnection发送cookie的正确方式吗?还是需要设置其他参数?