简介:该工具类使用HttpURLConnection实现了调用http请求,同时支持调用https请求。
1.实现及测试代码:
import com.lenovo.m2.test.utils.StringUtil;import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;/*** Created by admin on 2017/9/21.* 使用HttpURLConnection调用http接口*/
public class HttpURLConnectionUtil {
private static class MyX509TrustManager implements X509TrustManager {
@Overridepublic void checkClientTrusted(X509Certificate[] arg0, String arg1) {}@Overridepublic void checkServerTrusted(X509Certificate[] arg0, String arg1) {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}}/*** 该方法为调用https请求方法* @param url 请求的url* @param requestMethod 请求的方式:GET、POST* @param params 请求的参数:key1=value1&key2=value2* @return*/public static String requestHttps(String url,String requestMethod,String params){HttpsURLConnection connection = null;String result = "";try {// 创建SSLContext对象,并使用我们指定的信任管理器初始化TrustManager[] tm = { new MyX509TrustManager() };/*TrustManager[] tm = {new X509TrustManager() {@Overridepublic void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}}};*/SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");sslContext.init(null, tm, new SecureRandom());// 从上述SSLContext对象中得到SSLSocketFactory对象SSLSocketFactory ssf = sslContext.getSocketFactory();URL requestUrl = new URL(url);connection = (HttpsURLConnection)requestUrl.openConnection();//设置SSL签名connection.setSSLSocketFactory(ssf);//设置请求方式GET,POSTconnection.setRequestMethod(requestMethod);//设置是否使用缓存connection.setUseCaches(false);connection.setConnectTimeout(100000);connection.setReadTimeout(100000);if (!StringUtil.isEmpty(params)){//如果有参数,将参数写入connection.setDoOutput(true);connection.getOutputStream().write(params.getBytes("utf-8"));}InputStream in = connection.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));String line = null;while ((line=reader.readLine())!=null){result += line;}}catch (Exception e){e.printStackTrace();}finally {if (connection!=null){connection.disconnect();}}return result;}/*** 该方法为调用http请求方法* @param url 请求的url* @param requestMethod 请求的方式:GET、POST* @param params 请求的参数:key1=value1&key2=value2* @return*/public static String requestHttp(String url,String requestMethod,String params){HttpURLConnection connection = null;String result = "";try {URL requestUrl = new URL(url);connection = (HttpURLConnection)requestUrl.openConnection();//设置请求方式GET,POSTconnection.setRequestMethod(requestMethod);//设置是否使用缓存connection.setUseCaches(false);connection.setConnectTimeout(100000);connection.setReadTimeout(100000);if (!StringUtil.isEmpty(params)){//如果有参数,将参数写入connection.setDoOutput(true);connection.getOutputStream().write(params.getBytes("utf-8"));}InputStream in = connection.getInputStream();BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));String line = null;while ((line=reader.readLine())!=null){result += line;}}catch (Exception e){e.printStackTrace();}finally {if (connection!=null){connection.disconnect();}}return result;}public static void main(String[] args) {String requestMethod = "GET";System.out.println(requestHttp("http://www.baidu.com",requestMethod,null));System.out.println(requestHttps("https://www.baidu.com", requestMethod, null));}
}