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一直没有找到用法,不知道各位老大能不能给个例子,让我参考一下
谢谢了!!!!
在请求时被服务器拒绝,估计是写错了关于httpclient3.x的ntlm一直没有找到用法,不知道各位老大能不能给个例子,让我参考一下
谢谢了!!!!
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();
}
http://hc.apache.org/httpcomponents-client/ntlm.html提示信息如下
Server returned HTTP response code: 401 for URL: