HttpClient库出现Java-Http身份验证401错误
当我尝试从web浏览器(即Chromw等)登录时,会出现此对话框 使用ApacheHttpClient库尝试自动登录 路1<代码>http://userid:pass@主机名(X) 路2。使用ApacheHttpClient快速启动示例(X) 上述所有方法都不起作用 请帮帮我 最新尝试代码HttpClient库出现Java-Http身份验证401错误,java,apache,login,apache-httpclient-4.x,Java,Apache,Login,Apache Httpclient 4.x,当我尝试从web浏览器(即Chromw等)登录时,会出现此对话框 使用ApacheHttpClient库尝试自动登录 路1http://userid:pass@主机名(X) 路2。使用ApacheHttpClient快速启动示例(X) 上述所有方法都不起作用 请帮帮我 最新尝试代码 DefaultHttpClient httpClient = new DefaultHttpClient(); try{ httpClient.getCredentia
DefaultHttpClient httpClient = new DefaultHttpClient();
try{
httpClient.getCredentialsProvider()
.setCredentials(new AuthScope(
new HttpHost(host)),
new UsernamePasswordCredentials(username, password));
HttpGet httpget = new HttpGet(host);
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
控制台窗口显示:
'executing requestGET http://192.168.100.129/tag/unsubscribe HTTP/1.1'
'----------------------------------------'
'HTTP/1.0 401 Unauthorized'
'Response content length: 0'
这个代码对我有用
Base64(apache commons编解码器)出现了主要问题。
所以,我将Base64编码器更改为sun.misc.Base64编码器,它的工作就像魔术一样
最新工作代码
String enc = username + ":" + password;
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
HttpEntity entity;
HttpGet request = new HttpGet(host);
if(checkUserInfo()){
request.addHeader("Authorization", "Basic " + new BASE64Encoder().encode(enc.getBytes()));
httpResponse = client.execute(request);
entity = httpResponse.getEntity();
System.out.println(httpResponse.getStatusLine());
}else if(!checkUserInfo()){
throw new Exception("Error :: Must Call setUserInfo(String username, String password) methode firstly.");
}
控制台窗口最后说:
HTTP/1.0 200 OK
您能为您的尝试发布一些代码吗&可能是您得到的堆栈跟踪?如果你只是说“它不起作用”对不起,人们很难知道你做错了什么。刚刚编辑。我可以从哪里下载sun.misc.BASE64EnCoder.zip?@AnandRaj该编码器包括基本的jdk rt.jar文件jdk/jre/lib/rt.jar