[英]java HttpClient 403 forbidden problem?
我使用 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());
}
我有这个答案:
HTTP/1.1 403 Forbidden
Response content length: -1
但是使用浏览器,我可以使用相同的登录名和密码访问此页面!!!!
我该如何解决这个问题?
您构建 AuthScope object 并将端口参数设置为 443(HTTPS 的默认端口)。 但是,您创建了 HttpGet object,其中 URL 指向 HTTP(默认端口为 80)。
尝试使用以下方法构建 AuthScope:
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT)
或确保端口匹配。
检查您使用的HttpClient
版本是否是导致403
的原因。
尝试
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build();
您需要仔细查看浏览器实际上是如何进行身份验证的。
您要做的是(我认为)使用 HTTP 基本身份验证发送凭据。 如果站点设置为仅允许基于表单的身份验证和 session cookie,那么它将忽略包含凭据的 header。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.