java ----https

时间:2015-06-29 10:12:05   收藏:0   阅读:161
package com.token.common;


import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;


import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
 * httpsget
 * @author chenbin
 */
public class HttpsGetRequest{
private static class TrustAnyTrustManager implements X509TrustManager {


  public void checkClientTrusted(X509Certificate[] chain, String authType)
    throws CertificateException {
  }


  public void checkServerTrusted(X509Certificate[] chain, String authType)
    throws CertificateException {
  }


  public X509Certificate[] getAcceptedIssuers() {
   return new X509Certificate[] {};
  }
}


private static class TrustAnyHostnameVerifier implements HostnameVerifier {
  public boolean verify(String hostname, SSLSession session) {
   return true;
  }
}


@SuppressWarnings({ "deprecation", "null" })
public static String getReq(String url) throws Exception {
  InputStream in = null;
  OutputStream out = null;
  String str_return = "";
  try {
   SSLContext sc = SSLContext.getInstance("SSL");
   sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
     new java.security.SecureRandom());
   URL console = new URL(url);
     //"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1e07022a3ea97a52&secret=d398f72b43af0d4d03a2b38dd445fd31");
   HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
   conn.setSSLSocketFactory(sc.getSocketFactory());
   conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
   conn.connect();
   
   InputStream is = conn.getInputStream();
   DataInputStream indata = new DataInputStream(is);
   String ret = "";


   while (ret != null) {
    ret = indata.readLine();
    if (ret != null && !ret.trim().equals("")) {
     str_return = str_return
       + new String(ret.getBytes("ISO-8859-1"), "GBK");
    }
   }
   conn.disconnect();
  } catch (ConnectException e) {
   System.out.println("ConnectException");
   System.out.println(e);
   throw e;


  } catch (IOException e) {
   System.out.println("IOException");
   System.out.println(e);
   throw e;


  } finally {
   try {
    in.close();
   } catch (Exception e) {
   }
   try {
    out.close();
   } catch (Exception e) {
   }
  }
  return str_return;
}


}





package com.token.common;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


/**
 * httpspost
 * @author chenbin
 *
 */
public class HttpsPostRequest {
private static class TrustAnyTrustManager implements X509TrustManager {


  public void checkClientTrusted(X509Certificate[] chain, String authType)
    throws CertificateException {
  }


  public void checkServerTrusted(X509Certificate[] chain, String authType)
    throws CertificateException {
  }


  public X509Certificate[] getAcceptedIssuers() {
   return new X509Certificate[] {};
  }
}


private static class TrustAnyHostnameVerifier implements HostnameVerifier {
  public boolean verify(String hostname, SSLSession session) {
   return true;
  }
}


/**

* @param url ??????
* @param param post????????
* @return ???

原文:http://blog.csdn.net/u010598111/article/details/46673795

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!