123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- package com.chinacreator.videoalliance.order.util;
- import org.apache.commons.lang.StringUtils;
- import javax.net.ssl.KeyManager;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.TrustManager;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.security.SecureRandom;
- import java.util.HashMap;
- import java.util.Map;
- public class URLUtil {
- /**
- * 解读流
- * @param inStream
- * @return
- * @throws Exception
- */
- public static String readInputStream(InputStream inStream) throws Exception {
- ByteArrayOutputStream outStream = null;
- try {
- outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = inStream.read(buffer)) != -1) {
- outStream.write(buffer, 0, len);
- }
- byte[] data = outStream.toByteArray();
- return new String(data, "utf-8");
- } finally{
- if(outStream != null)
- outStream.close();
- if(inStream != null)
- inStream.close();
- }
- }
- /**
- * 以get方式获取链接的输出
- * @param href
- * @return
- * @throws Exception
- */
- public static String get(String href, int timeout) throws Exception {
- //改造
- try {
- SSLContext ctx = SSLContext.getInstance("TLSv1.2");
- ctx.init((KeyManager[])null, (TrustManager[])null, (SecureRandom)null);
- SSLContext.setDefault(ctx);
- } catch (NoSuchAlgorithmException var9) {
- var9.printStackTrace();
- } catch (KeyManagementException var10) {
- var10.printStackTrace();
- }
- //改造
- HttpURLConnection conn = null;
- try {
- URL url = new URL(href);
- conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- if(timeout > 0) {
- conn.setConnectTimeout(timeout);
- conn.setReadTimeout(timeout);
- }
- conn.addRequestProperty("content-type", "text/plain");
- conn.setUseCaches(false);
- conn.setDoOutput(false);
- return readInputStream(conn.getInputStream());
- } finally {
- if(conn != null) {
- conn.disconnect();
- }
- }
- }
- public static String get(String href) throws Exception {
- return get(href, 0);
- }
- /**
- * 以post方式获取链接的输出
- * @param href
- * @return
- * @throws Exception
- */
- public static String post(String href, String data, int timeout, String contentType) throws Exception {
- HttpURLConnection conn = null;
- OutputStreamWriter out = null;
- if(data == null || data.length() == 0) {
- return get(href, timeout);
- } else {
- try {
- URL url = new URL(href);
- conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("POST");
- if(timeout > 0) {
- conn.setConnectTimeout(timeout);
- conn.setReadTimeout(timeout);
- }
- conn.addRequestProperty("content-type", contentType);
- conn.setDoOutput(true);
- conn.setUseCaches(false);
- out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
- out.write(data);
- out.flush();
- } finally {
- if(out != null) {
- out.close();
- }
- if(conn != null) {
- conn.disconnect();
- }
- }
- if(conn != null) {
- return readInputStream(conn.getInputStream());
- }
- return null;
- }
- }
- public static String post(String href, String data, int timeout) throws Exception {
- return post(href, data, timeout, "text/plain");
- }
- public static String post(String href, String data) throws Exception {
- return post(href, data, 0);
- }
- public static String postJson(String href, String data) throws Exception {
- return post(href, data, 0, "application/json;charset=utf-8");
- }
- public static String postForm(String href, String data, int timeout) throws Exception {
- return post(href, data, timeout, "application/x-www-form-urlencoded");
- }
- public static String postForm(String href, String data) throws Exception {
- return postForm(href, data, 0);
- }
- /**
- * 把map转换为url地址
- * @param server 项目地址
- * @param map 存储参数的map
- * @return 返回完整的url地址
- */
- public static String mapToUrl(String server, Map<String,String> map){
- StringBuilder url = new StringBuilder(server);
- StringBuilder param = new StringBuilder("?");
- if (map == null) {
- return url.toString();
- }
- for(Map.Entry<String,String> entry: map.entrySet()){
- String key = entry.getKey();
- String val = entry.getValue();
- if(key.equalsIgnoreCase("action")){
- url.append(val).append(".do");
- } else {
- param.append(key).append("=").append(val).append("&");
- }
- }
- //删去最后的&
- if (param.toString().endsWith("&")) {
- param.deleteCharAt(param.length() - 1);
- }
- url.append(param);
- return url.toString();
- }
- /**
- * 把url的参数转换成Map
- * @param param 参数串
- * @return 返回存储Map
- */
- public static Map<String, String> urlParamsToMap(String param) {
- Map<String, String> map = new HashMap<String, String>(0);
- if (StringUtils.isBlank(param)) {
- return map;
- }
- String[] params = param.split("&");
- for (int i = 0; i < params.length; i++) {
- String[] p = params[i].split("=");
- if (p.length == 2) {
- map.put(p[0], p[1]);
- }
- }
- return map;
- }
- public static void main(String[] args) throws Exception {
- String url = "http://114.255.201.228:86/mobile-report/sendAlarmMsg.do";
- String result = postForm(url, "name=CDN_BUSINESS&content=测试短信");
- System.out.println(result);
- }
- }
|