我有一个安全的网址,如果我在浏览器中打开,会弹出一个对话框,并使用用户名/密码进行身份验证,然后下载pdf文件。
我想要Java中的此功能。它应该使用代理服务器/端口和用户详细信息进行身份验证,然后下载
URL server = new URL("http://some.site.url/download.aspx?client=xyz&docid=1001");
System.setProperty("https.proxyUser", userName);
System.setProperty("https.proxyPassword", password);
System.setProperty("https.proxyHost",proxy);
System.setProperty("https.proxyPort",port);
URLConnection connection = (URLConnection)server.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
//then i read this input stream and write to a pdf file in temporary folder
它给了我连接超时错误。
然后我想添加身份验证
String authentication = "Basic " + new
sun.misc.BASE64Encoder().encode("myuserid:mypassword".getBytes());
connection.setRequestProperty("Proxy-Authorization", authentication);
仍然不起作用,
请告诉我 。
我解决了这个问题。在连接URL之前,我使用了自定义的身份验证器,它可以对文档进行身份验证和下载。仅供参考-连接后,直到下一台服务器重新启动,它不需要身份验证。
URL server = new URL(url); //works for https and not for http, i needed https in my case.
Authenticator.setDefault((new MyAuthenticator()));
URLConnection connection = (URLConnection)server.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
.... //write code to fetch inputstream
如下定义您自己的身份验证器
public class MyAuthenticator extends Authenticator {
final PasswordAuthentication authentication;
public MyAuthenticator(String userName, String password) {
authentication = new PasswordAuthentication(userName, password.toCharArray());
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句