我正在尝试在 java 中模拟这种行为
curl -u usrname:password http://somewebsite.com/docs/DOC-2264
我不确定身份验证是 NTLM 还是 Basic。我登录到提供用户名和密码的网站。这是一个表格帖子。使用上面的 curl 命令,我可以登录并获取内容。
为了在java中做到这一点,我做了:
try {
URL url = new URL("http://somewebsite.com/docs/DOC-2264");
String authStr ="username:password";
String encodedAuthStr = Base64.encodeBytes(authStr.getBytes());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
onnection.setRequestProperty("Authorization", "Basic" + encodedAuthStr);
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in =
new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但我只得到登录页面。不是实际内容。我究竟做错了什么?