java HttpClient 403禁止问题?

发布于 2024-11-09 22:58:52 字数 748 浏览 3 评论 0原文

我使用 HttpClient api 对网站进行身份验证:

    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());
    }

我有这个答案:

HTTP/1.1 403 Forbidden
Response content length: -1 

但是使用浏览器,我可以使用相同的登录名和密码访问此页面!!!!

我该如何解决这个问题?

I using HttpClient api to authenticate to a web site:

    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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滿滿的愛 2024-11-16 22:58:53

检查您使用的 HttpClient 版本是否是导致 403 的原因。

尝试

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

Check if the Version of the HttpClient you are using is whats causing the 403.

Try

HttpClient httpClient = HttpClient.newBuilder()
            .version(HttpClient.Version.HTTP_1_1)
            .build();
谁的新欢旧爱 2024-11-16 22:58:52

您可以将端口参数设置为 443(HTTPS 的默认端口)来构造 AuthScope 对象。但是,您创建的 HttpGet 对象的 URL 指向 HTTP(默认端口 80)。

尝试使用以下方式构建 AuthScope:

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT) 

或确保端口匹配。

You construct the AuthScope object with the port parameter set to 443 (default port for HTTPS). However, you create the HttpGet object with the URL pointing to HTTP (with default port 80).

Either try to construct the AuthScope using:

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT) 

or make sure that ports will match.

旧伤还要旧人安 2024-11-16 22:58:52

您需要仔细查看浏览器实际上是如何进行身份验证的。

您想要做的是(我认为)使用 HTTP 基本身份验证发送凭据。如果站点设置为仅允许基于表单的身份验证和会话 cookie,那么它将忽略包含凭据的标头。

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文