1 package cn.xls; 2 3 import javax.net.ssl.HttpsURLConnection; 4 import javax.net.ssl.SSLContext; 5 import javax.net.ssl.SSLSocketFactory; 6 import javax.net.ssl.TrustManager; 7 import java.io.BufferedReader; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.io.OutputStream; 11 import java.net.ConnectException; 12 import java.net.HttpURLConnection; 13 import java.net.URL; 14 import java.security.SecureRandom; 15 16 /** 17 * @program: 18 * @description: 19 * @author: l 20 */ 21 public class HttpUrlConnectionDemo { 22 23 /** 24 * 发起http请求并获取结果 25 * 26 * @param requestUrl 请求地址 27 * @param requestMethod 请求方式 GET/POST 28 * @param outputStr 提交的数据 格式:(例子:"name=name&age=age) 29 * @return json字符串(json格式不确定 可能是JSONObject, 也可能是JSONArray, 这里用字符串在controller里再转化 30 */ 31 public static String httpRequest(String requestUrl, String requestMethod, String outputStr) { 32 String resultStr = ""; 33 StringBuffer buffer = new StringBuffer(); 34 try { 35 URL url = new URL(resultStr); 36 HttpURLConnection httpURLConn = (HttpURLConnection) url.openConnection(); 37 38 httpURLConn.setDoInput(true); 39 httpURLConn.setDoOutput(true); 40 httpURLConn.setUseCaches(false); 41 42 //设置请求方式 43 httpURLConn.setRequestMethod(requestMethod); 44 45 //设置超时时间 46 httpURLConn.setConnectTimeout(30 * 1000); 47 httpURLConn.setReadTimeout(10 * 1000); 48 49 //GET方式需要显示连接 50 if ("GET".equalsIgnoreCase(requestMethod)) { 51 httpURLConn.connect(); 52 } 53 54 //POST方式隐式自动连接 55 if (null != outputStr) { 56 OutputStream outputStream = httpURLConn.getOutputStream(); 57 //防止中文乱码 58 outputStream.write(outputStr.getBytes("UTF-8")); 59 outputStream.close(); 60 } 61 62 //将返回的输入流换成字符串 63 InputStream inputStream = httpURLConn.getInputStream(); 64 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); 65 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 66 67 String str = null; 68 while ((str = bufferedReader.readLine()) != null) { 69 buffer.append(str); 70 } 71 bufferedReader.close(); 72 inputStreamReader.close(); 73 //释放资源 74 inputStream.close(); 75 inputStream = null; 76 httpURLConn.disconnect(); 77 resultStr = buffer.toString(); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 } 81 return resultStr; 82 } 83 84 /** 85 * 发起https请求并获取结果 86 * 87 * @param requestUrl 请求地址 88 * @param requestMethod 请求方式 89 * @param outputStr 提交的数据 90 * @return json字符串 91 */ 92 public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) { 93 String resultStr = ""; 94 StringBuffer buffer = new StringBuffer(); 95 try { 96 //创建SSLcontext对象,并使用我们指定的信任管理器初始化 97 TrustManager[] trustManagers = {new MyX509TrustManager()}; 98 SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSEE"); 99 sslContext.init(null, trustManagers, new SecureRandom()); 100 //从上述SSLContext对象中得到SSLSocketFactory对象 101 SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); 102 103 URL url = new URL(resultStr); 104 HttpsURLConnection httpsUrlConn = (HttpsURLConnection) url.openConnection(); 105 httpsUrlConn.setSSLSocketFactory(sslSocketFactory); 106 107 httpsUrlConn.setRequestMethod(requestMethod); 108 httpsUrlConn.setConnectTimeout(30 * 1000); 109 httpsUrlConn.setReadTimeout(10 * 1000); 110 111 if ("GET".equalsIgnoreCase(requestMethod)) { 112 httpsUrlConn.connect(); 113 } 114 115 if (null != outputStr) { 116 OutputStream outputStream = httpsUrlConn.getOutputStream(); 117 outputStream.write(outputStr.getBytes("UTF-8")); 118 outputStream.close(); 119 } 120 121 InputStream inputStream = httpsUrlConn.getInputStream(); 122 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); 123 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 124 125 String str = null; 126 while ((str = bufferedReader.readLine()) != null) { 127 buffer.append(str); 128 } 129 bufferedReader.close(); 130 inputStreamReader.close(); 131 inputStream.close(); 132 inputStream = null; 133 httpsUrlConn.disconnect(); 134 135 resultStr = buffer.toString(); 136 } catch (ConnectException e) { 137 System.out.println("server connection timed out"); 138 e.printStackTrace(); 139 } catch (Exception e) { 140 e.printStackTrace(); 141 } 142 return resultStr; 143 } 144 145 public static void main(String[] args) { 146 // System.out.println(httpsRequest("https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&psort=3&page=3","GET","")); 147 System.out.println(httpRequest("http://bestcbooks.com/","GET",null)); 148 System.out.println(httpsRequest("https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&psort=3&page=3","GET",null)); 149 } 150 }
控制台:
java.net.MalformedURLException: no protocol: at java.net.URL.<init>(URL.java:586) at java.net.URL.<init>(URL.java:483) at java.net.URL.<init>(URL.java:432) at cn.xls.HttpUrlConnectionDemo.httpRequest(HttpUrlConnectionDemo.java:38) at cn.xls.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:150) java.security.NoSuchProviderException: no such provider: SunJSEE at sun.security.jca.GetInstance.getService(GetInstance.java:83) at sun.security.jca.GetInstance.getInstance(GetInstance.java:206) at javax.net.ssl.SSLContext.getInstance(SSLContext.java:199) at cn.xls.HttpUrlConnectionDemo.httpsRequest(HttpUrlConnectionDemo.java:101) at cn.xls.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:151)
请问这个该怎么解决啊?