关于使用httpclient进行ntlm认证的问题,

luanfengxia 2008-03-05 11:30:20
httpclient在3x版本中是支持ntlm认证的,但是在4.0版本中确不支持了,不知道是什么原因?

现在我用httpclient4.0版本做个客户端,但是需要设置代理,而代理需要ntlm认证的,我知道在jdk6.0以后的版本新的特性中支持ntlm的,不知道怎么才能让httpclient4.0也支持ntlm认证,希望各位能给出例子,谢谢!

同时使用httpclient3.x版本进行ntlm认证时,提示说是org.apache.commons.httpclient.HttpMethodDirector中的错误
在请求时被服务器拒绝,估计是写错了

关于httpclient3.x的ntlm一直没有找到用法,不知道各位老大能不能给个例子,让我参考一下


谢谢了!!!!
...全文
873 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
donglinlucky 2010-09-08
  • 打赏
  • 举报
回复
用的是jdk1.6、httpclient-4.0.1和jcifs-1.3.14访问ntlm认证的系统,在本机上是可以访问的,部署在linux服务器上,用了一次是可以的,后来不行了,在请求时被服务器拒绝,请问是什么原因呀?

主要技术参考是如下网址
http://hc.apache.org/httpcomponents-client/ntlm.html

提示信息如下
Server returned HTTP response code: 401 for URL:
qq_123 2008-06-27
  • 打赏
  • 举报
回复
Currently HttpClient 4.0 does not provide support for the NTLM authentication scheme out of the box and probably never will. The reasons for that are legal rather than technical.
forqzy 2008-03-07
  • 打赏
  • 举报
回复
另外一点是Apache的httpclient后来就不支持NTLM的1.3版本了,所以是没用的
forqzy 2008-03-07
  • 打赏
  • 举报
回复
这个问题倒是研究过一次,不过最后发现JDK1.5、1.6已实现了,所以没必要用Apache的httpclient了。

分为两种
1).你的机器是加入Domain的
2).一种是你的机器是不加入Domain的
private String openURL_NTLM(String urlString) throws IOException {
StringBuffer buf = new StringBuffer();
URL url = new URL(urlString);

Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("domain\\name", "pass".toCharArray());
}
});

InputStream in = url.openConnection().getInputStream();

while (in.available() > 0) {
byte[] b = new byte[in.available()];
int result = in.read(b);
if (result == -1) break;
buf.append(new String(b));
}
return buf.toString();
}

private String openURL_NTLM_Proxy(String urlString) throws IOException {
StringBuffer buf = new StringBuffer();
URL url = new URL(urlString);

Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("domain\\user", "pass".toCharArray());
}
});

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName("xx.xx.xx.xx"), 8080));
HttpURLConnection http = new HttpURLConnection(url, proxy);

http.setRequestProperty("Proxy-Authorization", new
BASE64Encoder().encode("domain\\user:pass".getBytes()));

http.connect();

InputStream in = http.getInputStream();
while (in.available() > 0) {
byte[] b = new byte[in.available()];
int result = in.read(b);
if (result == -1) break;
buf.append(new String(b));
}
return buf.toString();
}


bloodrate 2008-03-06
  • 打赏
  • 举报
回复
httpclient是什么东东?我一直把浏览器当作http的client。。。
老紫竹 2008-03-06
  • 打赏
  • 举报
回复
http://hc.apache.org/httpclient-3.x/authentication.html#Authentication_Schemes

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧