java使用代理請求https

我原本在我本機寫的代碼,本機電腦是能夠連外網沒限制,對於https和http均可以。可是放在linux服務器上後,由於VM限制了不能訪問外網,並且有ssl驗證因此就一直報錯,要麼是連不上線上請求,要麼是訪問不了SSL 443端口,其實這2錯同樣。java

package util_liyuan;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;

public class Tools {

    private static boolean proxySet = true;
    private static String proxyHost = "192.168.11.254";
    private static int proxyPort = 8080;
	public static final String GET_URL = "https://csc.pconline.com.cn/s/buy/search/getSkuByLink.xsp";
    public static void main(String[] args) {
    	List<String> params = readFile02("E:\\360downloads\\111\\urls.txt");
        sendGet(params, proxySet);
    }
  //讀取本地文件pos.txt
  	public static List<String> readFile02(String path)  {
          // 使用一個字符串集合來存儲文本中的路徑 ,也可用String []數組
          List<String> list = new ArrayList<String>();
          try {
  			FileInputStream fis = new FileInputStream(path);
  			// 防止路徑亂碼   若是utf-8 亂碼  改GBK     eclipse裏建立的txt  用UTF-8,在電腦上本身建立的txt  用GBK
  			InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
  			BufferedReader br = new BufferedReader(isr);
  			String line = "";
  			while ((line = br.readLine()) != null) {
  				// 若是 t x t文件裏的路徑 不包含---字符串       這裏是對裏面的內容進行一個篩選
  				list.add(line);
  			}
  			br.close();
  			isr.close();
  			fis.close();
  		} catch (Exception e) {
  			e.printStackTrace();
  		}
          return list;
      }
    //設置請求頭屬性
    public static Map<String, String> setProperty() {
        HashMap<String, String> pMap = new HashMap<>();
        // pMap.put("Accept-Encoding", "gzip"); //請求定義gzip,響應也是壓縮包
        pMap.put("connection", "Keep-Alive");
        pMap.put("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        pMap.put("Content-Type", "application/x-www-form-urlencoded");
        return pMap;
    }

    /**
     * GET請求
     * 
     * @param url
     *            請求的URL
     * @param param
     *            請求參數,name1=value1&name2=value2 的形式
     * @return 響應結果
     */
    public static StringBuffer sendGet( List<String> params, boolean isproxy) {
        //String result = "";
        BufferedReader in = null;
        StringBuffer sb = new StringBuffer();
        try {
            for (int i = 0; i < params.size(); i++) {
            	URL realUrl = new URL(GET_URL+ "?buyLink=" + params.get(i));
            	// 打開和URL之間的鏈接
                HttpURLConnection connection = null;
                if (isproxy) {// 使用代理模式
                    @SuppressWarnings("static-access")
                    Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                    connection = (HttpURLConnection) realUrl.openConnection(proxy);
                } else {
                    connection = (HttpURLConnection) realUrl.openConnection();
                }

                // https 忽略證書驗證
                if (params.get(i).substring(0, 5).equals("https")) {
                    SSLContext ctx = MyX509TrustManagerUtils();
                    ((HttpsURLConnection) connection).setSSLSocketFactory(ctx.getSocketFactory());
                    ((HttpsURLConnection) connection).setHostnameVerifier(new HostnameVerifier() {
                        @Override
                        public boolean verify(String arg0, SSLSession arg1) {
                            return true;
                        }
                    });
                }

                // 設置通用的請求屬性
                for (Map.Entry<String, String> entry : setProperty().entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
                // 創建鏈接
                connection.connect();
                // 定義 BufferedReader輸入流來讀取URL的響應
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK
                        || connection.getResponseCode() == HttpURLConnection.HTTP_CREATED
                        || connection.getResponseCode() == HttpURLConnection.HTTP_ACCEPTED) {
                    in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                } else {
                    in = new BufferedReader(new InputStreamReader(connection.getErrorStream(), "UTF-8"));
                }
                //System.out.println(in);
                String line;
                while ((line = in.readLine()) != null) {
                	if (line.contains("success")) {
						sb.append(GET_URL + "?buyLink=" + params.get(i)+"解析成功"+"\n");
						//jieguo.add(GET_URL + "?buyLink=" + params.get(i)+"解析成功");
						System.out.println((i+1)+GET_URL + "?buyLink=" + params.get(i)+"解析成功");
					}else {
						sb.append(GET_URL + "?buyLink=" + params.get(i)+"解析失敗"+"\n");
						//jieguo.add(GET_URL + "?buyLink=" + params.get(i)+"解析失敗");
						System.out.println((i+1)+GET_URL + "?buyLink=" + params.get(i)+"解析失敗");
					}
                }
			}
            
            
            
        } catch (Exception e) {
            System.out.println("發送GET請求出現異常!");
            e.printStackTrace();
        }
        // 使用finally塊來關閉輸入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return sb;
    }

    /**
     * POST請求
     * 
     * @param url
     *            發送請求的 URL
     * @param param
     *            請求參數 name1=value1&name2=value2 的形式
     * @param isproxy
     *            是否使用代理模式
     * @return 響應結果
     */
    public static String sendPost(String url, String param, boolean isproxy) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = null;
            if (isproxy) {// 使用代理模式
                @SuppressWarnings("static-access")
                Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                conn = (HttpURLConnection) realUrl.openConnection(proxy);
            } else {
                conn = (HttpURLConnection) realUrl.openConnection();
            }

            // https
            if (url.substring(0, 5).equals("https")) {
                SSLContext ctx = MyX509TrustManagerUtils();
                ((HttpsURLConnection) conn).setSSLSocketFactory(ctx.getSocketFactory());
                ((HttpsURLConnection) conn).setHostnameVerifier(new HostnameVerifier() {
                    //在握手期間,若是 URL 的主機名和服務器的標識主機名不匹配,則驗證機制能夠回調此接口的實現程序來肯定是否應該容許此鏈接。
                    @Override
                    public boolean verify(String arg0, SSLSession arg1) {
                        return true;
                    }
                });
            }

            // 發送POST請求必須設置以下兩行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST"); // POST方法

            // 設置通用的請求屬性

            for (Map.Entry<String, String> entry : setProperty().entrySet()) {
                conn.setRequestProperty(entry.getKey(), entry.getValue());
            }
            conn.connect();

            // 獲取URLConnection對象對應的輸出流
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            // 發送請求參數
            out.write(param);
            // flush輸出流的緩衝
            out.flush();
            // 定義BufferedReader輸入流來讀取URL的響應
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK
                    || conn.getResponseCode() == HttpURLConnection.HTTP_CREATED
                    || conn.getResponseCode() == HttpURLConnection.HTTP_ACCEPTED) {
                in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            } else {
                in = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
            }
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("發送 POST 請求出現異常!");
            e.printStackTrace();
        }
        // 使用finally塊來關閉輸出流、輸入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    // ===========================utils===================

    /**
     * url編碼
     * 
     * @param source
     *            待編碼字符串
     * @param encode
     *            字符編碼 eg:UTF-8
     * @return 編碼字符串
     */
    public static String urlEncode(String source, String encode) {
        String result = source;
        try {
            result = java.net.URLEncoder.encode(source, encode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "0";
        }
        return result;
    }

    /*
     * HTTPS忽略證書驗證,防止高版本jdk因證書算法不符合約束條件,使用繼承X509ExtendedTrustManager的方式
     */
    class MyX509TrustManager extends X509ExtendedTrustManager {

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // TODO Auto-generated method stub

        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // TODO Auto-generated method stub

        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1, Socket arg2) throws CertificateException {
            // TODO Auto-generated method stub

        }

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1, SSLEngine arg2)
                throws CertificateException {
            // TODO Auto-generated method stub

        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1, Socket arg2) throws CertificateException {
            // TODO Auto-generated method stub

        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1, SSLEngine arg2)
                throws CertificateException {
            // TODO Auto-generated method stub

        }

    }

    public static SSLContext MyX509TrustManagerUtils() {

        TrustManager[] tm = { new HttpsUtils().new MyX509TrustManager() };
        SSLContext ctx = null;
        try {
            ctx = SSLContext.getInstance("TLS");
            ctx.init(null, tm, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ctx;
    }

}
相關文章
相關標籤/搜索