有一个在线文件(例如http://www.example.com/information.asp)我需要抓取并保存到目录中.我知道有几种方法可以逐行获取和读取在线文件(URL),但有没有办法只使用Java下载和保存文件?
我知道,关于这个问题有很多不同的问题和很多答案...但我无法理解......
我有:ubuntu-9.10-desktop-amd64 + NetBeans6.7.1从"关闭"安装.代表.我需要通过HTTPS连接到某个站点.为此我使用Apache的HttpClient.
从教程我读到:
"一旦正确安装了JSSE,通过SSL进行安全的HTTP通信
就像普通的HTTP通信一样简单." 还有一些例子:
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("https://www.verisign.com/");
try {
httpclient.executeMethod(httpget);
System.out.println(httpget.getStatusLine());
} finally {
httpget.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)
到现在为止,我写道:
HttpClient client = new HttpClient();
HttpMethod get = new GetMethod("https://mms.nw.ru");
//get.setDoAuthentication(true);
try {
int status = client.executeMethod(get);
System.out.println(status);
BufferedInputStream is = new BufferedInputStream(get.getResponseBodyAsStream());
int r=0;byte[] buf = new byte[10];
while((r = is.read(buf)) > 0) {
System.out.write(buf,0,r);
}
} catch(Exception ex) {
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
结果我有一组错误:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable …Run Code Online (Sandbox Code Playgroud) 我正在使用Java访问HTTPS站点,该站点以XML格式返回显示.我在URL本身传递登录凭据.这是代码片段:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
requestURL = "https://Administrator:Password@localhost:8443/abcd";
try {
InputStream is = null;
URL url = new URL(requestURL);
InputStream xmlInputStream =new URL(requestURL).openConnection().getInputStream();
byte[] testByteArr = new byte[xmlInputStream.available()];
xmlInputStream.read(testByteArr);
System.out.println(new String(testByteArr));
Document doc = db.parse(xmlInputStream);
System.out.println("DOC="+doc);
} catch (MalformedURLException e) {
}
Run Code Online (Sandbox Code Playgroud)
我正在该程序中创建一个不验证签名/未签名证书的信任管理器.但是,在运行上述程序时,我收到错误服务器返回的HTTP响应代码:401 for URL:https:// Administrator:Password @ localhost:8443/abcd
我可以在浏览器上使用相同的URL,并正确显示xml.请告诉我如何在Java程序中完成这项工作.
我已经编写了一些代码来从网站下载文件.该代码适用于测试http网址.一旦我将URL更改为https,我就会有一个连接超时.
System.setProperty("http.proxyHost","trproxy.rwe.com") ;
System.setProperty("http.proxyPort", "80") ;
Authenticator.setDefault (new MyAuthenticator("USER","PW"));
//URL url = new URL("http","www.treasury.gov",80,"/ofac/downloads/sdn.csv",new sun.net.www.protocol.http.Handler()); THIS WORKS
URL url = new URL("https", "downloads.elexonportal.co.uk",443,"/bmradataarchive/download?key=MYKEY&filename="+filename,new sun.net.www.protocol.https.Handler());
url.openConnection();
InputStream reader = url.openStream();
FileOutputStream writer = new FileOutputStream("C:/downloads/"+filename);
Run Code Online (Sandbox Code Playgroud)
如果我将https网址复制到浏览器中,我会被问到我希望保存文件的位置,并且工作正常.任何帮助非常感谢.我试过这个 但是没用
谢谢克里斯