java中使用http下载文件需要用户名和密码(求解决)

pfgmylove 2011-09-05 10:30:28
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//String authString = "pfg" + ":" + "123";
byte [] b1 = authString.getBytes();
String auth = "Basic " +Base64Coder.getInstance().encode(b1, 0, b1.length);
//connection.setRequestProperty("Proxy-Authorization", auth); // 我试过了这种方法,但是不行。//连接指定的资源
connection.connect();
InputStream is = connection.getInputStream();
...全文
677 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
softroad 2011-09-05
  • 打赏
  • 举报
回复
这是基础的http协议
softroad 2011-09-05
  • 打赏
  • 举报
回复

if(username != null && password != null) {
String author = new sun.misc.BASE64Encoder()
.encode((username + ":" + password).getBytes());
conn.setRequestProperty("Authorization", "Basic " + author);
}
pfgmylove 2011-09-05
  • 打赏
  • 举报
回复
我自己已解决这个问题。
使用下面的方法
Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("pioneer", "ind4sh".toCharArray());
}
});
先进者 2011-09-05
  • 打赏
  • 举报
回复
说一下,界面会出现什么样的画面?
小笨熊 2011-09-05
  • 打赏
  • 举报
回复
你现在的问题是啥?可以登录成功吗?
pfgmylove 2011-09-05
  • 打赏
  • 举报
回复
楼上的你这个方法不行,我刚开始的时候就用过了,不行。
应该用:
Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("username", "password".toCharArray());
}
});

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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