我对java比较陌生,在登录后一直在尝试获取网页的源代码(网站是https://stem7.maxnet.co.nz/ispcentre/home.php)
我使用的代码是我试图扩展教程。(可能太远了)下面的代码返回登录页面的源代码,到此为止。
private String getSource(URL url) throws IOException {
HttpsURLConnection spoof = (HttpsURLConnection)url.openConnection();
StringBuffer sb = new StringBuffer();
String basicAuth = "Basic " + Base64.encodeToString("username:password".getBytes(), Base64.DEFAULT);
spoof.setRequestProperty ("Authorization", basicAuth);
spoof.setRequestMethod("POST");
spoof.setUseCaches(false);
spoof.setDoInput(true);
spoof.setDoOutput(true);
spoof.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)");
BufferedReader in = new BufferedReader(new InputStreamReader(
spoof.getInputStream()));
String strLine = "";
while ((strLine = in.readLine()) != null) {
sb.append(strLine);
}
return sb.toString();
}任何帮助都将不胜感激。甚至是在正确的方向上的一点。干杯
发布于 2017-03-22 09:23:21
该页面使用表单身份验证,这意味着当您登录时,后端希望请求包含两个参数,一个用于用户名,一个用于密码,但您的代码使用的是基本身份验证,每次都会失败,这就是为什么您一直在获取登录页面的源代码(服务器无法对您进行身份验证,因此它会将客户端重定向到登录页面,从而将登录页面的HTML源代码返回给您。所以你可以试着这样做
connection.setRequestProperty("username", "username");
connection.setRequestProperty("password", "password");https://stackoverflow.com/questions/42940994
复制相似问题