我正在实现单点登录功能,以便使用摘要身份验证自动登录到附属的 https 网站。目前我的代码是
URL url = new URL(protocol, ip, port, path);
URLConnection connection = url.openConnection(Proxy.NO_PROXY);
connection.connect();
if (connection != null && connection.getHeaderFields() != null) {
if (connection.getHeaderFields().get(AUTHENTICATE_RESPONSE_HEADER) != null) {
Map<String, String> authenticateParameters = identifyAuthentication(connection);
String ha1 = calculateMD5(username + ":" + authenticateParameters.get("realm") + ":" + password);
String ha2 = calculateMD5("GET" + ":" + path);
String response = calculateMD5(ha1 + ":" +
authenticateParameters.get("nonce") + ":" +
"00000001" + ":" +
authenticateParameters.get("qop") + ":" +
ha2);
String authorizationRequest = authenticateParameters.get("challenge") + " " +
"username=" + username + ", " +
"realm=" + authenticateParameters.get("realm") + ", " +
"nonce=" + authenticateParameters.get("nonce") + ", " +
"uri=" + path + ", " +
"qop=" + authenticateParameters.get("qop") + ", " +
"nc=" + "00000001" + ", " +
"response=" + response + ", " +
"opaque=" + authenticateParameters.get("opaque");
connection.setAllowUserInteraction(true);
connection.addRequestProperty(AUTHENTICATION_REQUEST_PROPERTY, authorizationRequest);
connection.getHeaderFields();
}
}
问题是我得到
java.lang.IllegalStateException: Already connected
at java.net.URLConnection.addRequestProperty(URLConnection.java:1061)
at sun.net.www.protocol.http.HttpURLConnection.addRequestProperty(HttpURLConnection.java:2016)
at com.ibm.net.ssl.www2.protocol.https.a.addRequestProperty(a.java:49)
我想这是有道理的,但对我没有帮助。我将如何创建一个请求/响应以在此处登录(并最终获得一个 sessionId)?
提前致谢。