gpt4 book ai didi

java - Java 上的 HTML 身份验证不起作用

转载 作者:可可西里 更新时间:2023-11-01 16:18:54 26 4
gpt4 key购买 nike

我正在尝试使用 Apache 的一些 jar 文件向网站发送我的用户名或密码,并尝试借助我的方法“loadpage”从该网站读取所有内容。

这是行不通的。在我执行我的方法“loadpage”之后。我仍然从主页获得 Streams,我不需要登录。我想在登录后拥有 Streams。

  public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("https://justawebsite", 8080),
new UsernamePasswordCredentials("username","password"));

HttpGet httpget = new HttpGet("https://justawebsite");

System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
}

我也在没有 Apache 的一些 jarfile 的帮助下尝试了它。

public  void LogIn(String url1) throws Exception{
URL url = new URL(url1);

String userPassword = "username"+":"+"password";
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
//con.setRequestProperty("Cookie", "JSESSIONID="+getSid());

URLConnection con = url.openConnection();
//con.connect();
con.setRequestProperty("Cookie", "JSESSIONID=" + encoding);


con.connect();

}

我的方法 Loadpage:之所以有效,是因为我在其他一些不需要身份验证的网站上尝试过。

public String loadPage(String url) 抛出异常{

    URLConnection con = new URL(url).openConnection();
StringBuilder buffer = new StringBuilder();

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while((line=in.readLine())!= null){
buffer.append(line);
}
in.close();
return buffer.toString();

}

最佳答案

您应该首先检查 Behe 指出的网站使用的身份验证类型,因为有不同的 Web Authentication Types .

您的第一个示例是使用基本身份验证,但该网站可能需要某种基于表单的身份验证。

如果最新的是你的情况,你应该检查这个 Client HTTP Programming Primer其中包括

  • enter username and password in a web form and hit the "login" button

关于java - Java 上的 HTML 身份验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10388603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com