我必须通过我的 java 代码访问 https 站点。但它返回 401 响应。我在下面包含了我的代码。
try {
URL u = new URL(url);
HttpURLConnection http = (HttpURLConnection)u.openConnection();
http.setAllowUserInteraction(true);
http.connect();
String userpassword = "HP:M0lveau";
byte[] encoded = Base64.encodeBase64(userpassword.getBytes());
String encodedAuthorization = new String(encoded);
http.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
InputStream is = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
return stringBuilder.toString();
} catch (IOException ioe) {
logger.debug("fetchDataFromServer:IOException");
return null;
}
请尽早提供帮助..提前谢谢....