简体   繁体   English

java HttpClient 403禁止问题?

[英]java HttpClient 403 forbidden problem?

I using HttpClient api to authenticate to a web site:我使用 HttpClient api 对 web 站点进行身份验证:

    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(AuthScope.ANY_HOST, 443),
            new UsernamePasswordCredentials(args[0], args[1]));

    HttpGet httpget = new HttpGet("http://..........");

    HttpResponse response = httpclient.execute(httpget);

    System.out.println(response.getStatusLine());
    if (entity != null) {
        System.out.println("Response content length: "
                + entity.getContentLength());
    }

I have this answer:我有这个答案:

HTTP/1.1 403 Forbidden
Response content length: -1 

But with a browser i have access to this page with the same login and password !!!!但是使用浏览器,我可以使用相同的登录名和密码访问此页面!!!!

How can i fix this problem?我该如何解决这个问题?

You construct the AuthScope object with the port parameter set to 443 (default port for HTTPS).您构建 AuthScope object 并将端口参数设置为 443(HTTPS 的默认端口)。 However, you create the HttpGet object with the URL pointing to HTTP (with default port 80).但是,您创建了 HttpGet object,其中 URL 指向 HTTP(默认端口为 80)。

Either try to construct the AuthScope using:尝试使用以下方法构建 AuthScope:

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT) 

or make sure that ports will match.或确保端口匹配。

Check if the Version of the HttpClient you are using is whats causing the 403 .检查您使用的HttpClient版本是否是导致403的原因。

Try尝试

HttpClient httpClient = HttpClient.newBuilder()
            .version(HttpClient.Version.HTTP_1_1)
            .build();

You need to look carefully at how the browser is actually authenticating.您需要仔细查看浏览器实际上是如何进行身份验证的。

What you are trying to do is (I think) send the credentials using HTTP Basic Authentication.您要做的是(我认为)使用 HTTP 基本身份验证发送凭据。 If the site is set up to only allow form-based authentication and a session cookie, then it will ignore the header containing the credentials.如果站点设置为仅允许基于表单的身份验证和 session cookie,那么它将忽略包含凭据的 header。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM