package com.yixun.wap;
/** * 本程序可用于备份百度空间的文章。 * 18 hours costed. * niuys. * 2007.7.11 */ import java.io.*;
import java.net.*;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import java.util.*;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
publicclass OpticalBackup
{ // 把博客信息应编码 private String urlString = "https://passport.baidu.com/?login"; // 保存的目录 private String saveDir = "L:\\blogBackup"; // 博客的根路径 private String spaceRoot = "http://hi.baidu.com/niuys/blog/index/"; private String articleURLHead = "/niuys/blog/item/"; // parameters needed to log in. Map parameters = new HashMap(); // 建立连接需要使用cookie提供的sessionID. String cookieVal = null; // Create an anonymous class to trust all certificates. // This is bad style, you should create a separate class. private X509TrustManager xtm = new X509TrustManager() { publicvoid checkClientTrusted(X509Certificate[] chain, String authType) { } publicvoid checkServerTrusted(X509Certificate[] chain, String authType) { // System.out.println("cert: " + chain[0].toString() + ", authType: // " // + authType); } public X509Certificate[] getAcceptedIssuers() { returnnull; } }; // Create an class to trust all hosts private HostnameVerifier hnv = new HostnameVerifier() { publicboolean verify(String hostname, SSLSession session) { // System.out.println("hostname: " + hostname); returntrue; } }; // In this function we configure our system with a less stringent // hostname verifier and X509 trust manager. This code is // executed once, and calls the static methods of HttpsURLConnection public OpticalBackup() { // The parameters needed to log on. parameters.put("username", "保密"); parameters.put("password", "保密"); parameters.put("Submit", " 登录 "); parameters.put("tpl", "sp"); parameters.put("tpl_reg", "sp"); parameters.put("u", "http://www.baidu.com/"); // check the saveDir File dir = new File(saveDir); if (!dir.exists()) { dir.mkdir(); } // Initialize the TLS SSLContext with // our TrustManager SSLContext sslContext = null; try{ sslContext = SSLContext.getInstance("TLS"); X509TrustManager[] xtmArray = new X509TrustManager[] {xtm}; sslContext.init(null, xtmArray, new java.security.SecureRandom()); }catch (GeneralSecurityException e) { // Print out some error message and deal with this exception e.printStackTrace(); } // Set the default SocketFactory and HostnameVerifier // for javax.net.ssl.HttpsURLConnection if (sslContext != null) { HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); } HttpsURLConnection.setDefaultHostnameVerifier(hnv); } // the whole process publicvoid run() { try{ URL url = new URL(urlString); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // Set properties of the connection urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // Form the POST parameters StringBuilder content = new StringBuilder(); boolean first = true; Set set = parameters.entrySet(); Iterator iterator = set.iterator(); Map.Entry parameter = (Map.Entry) iterator.next(); try{ while (parameter != null) { if (!first) { content.append("&"); } content.append(URLEncoder.encode((String) parameter.getKey(), "UTF-8")).append("="); content.append(URLEncoder.encode((String) parameter.getValue(), "UTF-8")); first = false; parameter = (Map.Entry) iterator.next(); } }catch (NoSuchElementException e) { e.printStackTrace(); } // send the POST request to server OutputStream outputStream = null; try{ outputStream = urlConnection.getOutputStream(); outputStream.write(content.toString().getBytes("utf-8")); outputStream.flush(); }finally{ if (outputStream != null) { outputStream.close(); } } // Retrieve the output InputStream inputStream = null; StringBuilder outputBuilder = new StringBuilder(); try{ int responseCode = urlConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = urlConnection.getInputStream(); }else{ inputStream = urlConnection.getErrorStream(); } String string; if (inputStream != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); while (null != (string = reader.readLine())) { outputBuilder.append(string).append('\n'); } } // get the cookie value in the header filelds Map map = urlConnection.getHeaderFields(); Collection collection = map.values(); Object[] values = collection.toArray(); // values[4].toString() is nedded .Otherwise,an exception // throws. Direct class cast is forbidded. cookieVal = (String) values[4].toString(); // "replaceAll()" requests that if "[" exits, "]" is nedded // too.This is the diffrence from "replace()". cookieVal = cookieVal.replace(']', ' ').replace("[PASSPORTRETRYSTRING=deleted; expires=Mon, 01 Jan 1970 00:00:00 GMT; path=/;", "");// remedy the first key from cookie }finally{ if (inputStream != null) { inputStream.close(); } } // find all the URLs of the articles you write. ArrayList pageURLs = findLinkers(); // save all the articles for (int i = 0; i < pageURLs.size(); i++) { savePage((String) pageURLs.get(i)); } }catch (Exception e) { e.printStackTrace(); } } // save the article with the URL privatevoid savePage(String pageURL) { try{ // create the file to accept the output.The file's name is a part of // the URL File pageFile = new File(saveDir + "\\" + pageURL.substring(36)); // 增量备份 if (pageFile.exists()) return; FileWriter fw = new FileWriter(pageFile); // create the connection to the server,and get the results. URL url = new URL(pageURL); String content = getContentFromURL(url); // write the contents to the file. fw.write(content); // flush the contents in the buffer.Without // it,the file's content may be not completed. fw.flush(); fw.close(); }catch (Exception e) { e.printStackTrace(); } } // find all the articles' URL from the html code accepted from the server private ArrayList findLinkers() { // the index of the articles int i = 0; ArrayList linkers = new ArrayList(); URL url = null; try{ while (true) { url = new URL(spaceRoot + i); System.out.println("url==" + url); i++;// next index // get the content from server String content = getContentFromURL(url); // Analyze the content. ArrayList temp = getLinkerFromContent(content); if (temp.isEmpty())// no article url exists return linkers; linkers.addAll(temp);// combine the URLs } }catch (Exception e) { e.printStackTrace(); } return linkers; } private ArrayList getLinkerFromContent(String content) { ArrayList linkerArray = new ArrayList(); String linker = null; // The article's URL begins with "/niuys/blog/item/" int index = content.indexOf(articleURLHead); if (content.indexOf("暂无文章") > 0) {// 超过页数时会返回包含“暂无文章”的页,end return linkerArray; } // deal with the content while (index > 0) { // get the whole URL linker = content.substring(index, index + 46); content = content.replace(linker, ""); index = content.indexOf(articleURLHead); linker = "http://hi.baidu.com" + linker; linkerArray.add(linker); System.out.println("linker==" + linker); } return linkerArray; } private String getContentFromURL(URL url) throws Exception { HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // Set properties of the connection urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); urlConnection.addRequestProperty("Cookie", cookieVal); // Retrieve the output InputStream inputStream = null; StringBuilder outputBuilder = new StringBuilder(); int responseCode = urlConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = urlConnection.getInputStream(); }else{ inputStream = urlConnection.getErrorStream(); } String string; if (inputStream != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); while (null != (string = reader.readLine())) { outputBuilder.append(string).append('\n'); } inputStream.close(); return outputBuilder.toString(); } return ""; } publicstaticvoid main(String[] args) { OpticalBackup backup = new OpticalBackup(); backup.run(); } }