java HttpClient 403禁止问题?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查您使用的
HttpClient
版本是否是导致403
的原因。尝试
Check if the Version of the
HttpClient
you are using is whats causing the403
.Try
您可以将端口参数设置为 443(HTTPS 的默认端口)来构造 AuthScope 对象。但是,您创建的 HttpGet 对象的 URL 指向 HTTP(默认端口 80)。
尝试使用以下方式构建 AuthScope:
或确保端口匹配。
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:
or make sure that ports will match.
您需要仔细查看浏览器实际上是如何进行身份验证的。
您想要做的是(我认为)使用 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.