123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- package com.chinacreator.videoalliance.order;
- import org.apache.log4j.Logger;
- import javax.net.ssl.*;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.SocketTimeoutException;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- public class HttpInvoke {
-
- protected static Logger log = Logger.getLogger(HttpInvoke.class);
-
- 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;
- }
- }
-
- /**
- * 处理https请求
- * @param url
- * @param content
- * @param reqProperty
- * @return
- */
- public static String sendhttpsReq(String method, String url, String content, Map reqProperty){
- StringBuffer buffer = new StringBuffer();
- HttpsURLConnection connection = null;
- BufferedReader reader = null;
- OutputStream out = null;
- InputStream inputStream = null;
- InputStreamReader inputStreamReader = null;
- try {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
- connection = (HttpsURLConnection) new URL(url).openConnection();
- connection.setSSLSocketFactory(sc.getSocketFactory());
- connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
- connection.setRequestMethod(method);
- connection.setDoOutput(true);
- connection.setDoInput(true);
- //设置超时时间为30秒
- connection.setConnectTimeout(30000);
- connection.setReadTimeout(30000);
-
- if(reqProperty != null && reqProperty.size()>0){
- Set keySet = reqProperty.keySet();
- Iterator iterator = keySet.iterator();
- String key = "";
- String value = "";
- while(iterator.hasNext()){
- key = (String)iterator.next();
- value = (String)reqProperty.get(key);
- connection.setRequestProperty(key, value);
- }
- }
-
- connection.connect();
- out = connection.getOutputStream();
- out.write(content.getBytes("UTF-8"));
- out.flush();
-
- if (200 != connection.getResponseCode()) {
- }else{
- inputStream = connection.getInputStream();
- inputStreamReader = new InputStreamReader(inputStream, "utf-8");
- reader = new BufferedReader(inputStreamReader);
- String line = "";
- while ((line = reader.readLine()) != null) {
- buffer.append(line);
- }
- reader.close();
- }
- } catch (SocketTimeoutException e){
- log.error("调HTTP请求出现超时:"+e.getMessage());
- buffer.append("调HTTP请求出现超时异常:"+e.getMessage());
- } catch (IOException e) {
- log.error("调HTTP请求出现IO异常:"+e.getMessage());
- buffer.append("调HTTP请求出现IO异常:"+e.getMessage());
- } catch (Exception e){
- log.error("调HTTP请求出现异常:"+e.getMessage());
- buffer.append("调HTTP请求出现异常:"+e.getMessage());
- } finally {
- if(inputStreamReader != null){
- try {
- inputStreamReader.close();
- } catch (IOException e) {
- inputStreamReader = null;
- log.error("释放inputStreamReader异常:"+e.getMessage());
- }
- }
- if(inputStream != null){
- try {
- inputStream.close();
- } catch (IOException e) {
- inputStream = null;
- log.error("释放inputStream异常:"+e.getMessage());
- }
- }
- if(out != null){
- try {
- out.close();
- } catch (IOException e) {
- out = null;
- log.error("释放out异常:"+e.getMessage());
- }
- }
- if(reader != null){
- try {
- reader.close();
- } catch (IOException e) {
- reader = null;
- log.error("释放reader异常:"+e.getMessage());
- }
- }
- if (connection != null) {
- connection.disconnect();
- }
- }
- return buffer.toString();
- }
-
- /**
- * 连接到服务器并获取数据
- * GET方式发起请求
- * @param urlStr 请求URL
- * @param content 请求报文
- * @return
- */
- public static String sendHttpByGet(String urlStr, String content, Map reqProperty) {
- URL url = null;
- HttpURLConnection connection = null;
- StringBuffer buffer = new StringBuffer();
- BufferedReader reader = null;
- OutputStream out = null;
- InputStream inputStream = null;
- InputStreamReader inputStreamReader = null;
- try {
- url = new URL(urlStr+"/"+ URLEncoder.encode(content,"UTF-8"));
- connection = (HttpURLConnection) url.openConnection();
-
- if(reqProperty != null && reqProperty.size()>0){
- Set keySet = reqProperty.keySet();
- Iterator iterator = keySet.iterator();
- String key = "";
- String value = "";
- while(iterator.hasNext()){
- key = (String)iterator.next();
- value = (String)reqProperty.get(key);
- connection.setRequestProperty(key, value);
- }
- }
- //设置超时时间为30秒
- connection.setConnectTimeout(30000);
- connection.setReadTimeout(30000);
- connection.connect();
- if (200 != connection.getResponseCode()) {
-
- }else{
- inputStream = connection.getInputStream();
- inputStreamReader = new InputStreamReader(inputStream, "utf-8");
- reader = new BufferedReader(inputStreamReader);
- String line = "";
- while ((line = reader.readLine()) != null) {
- buffer.append(line);
- }
- reader.close();
- }
- } catch (SocketTimeoutException e){
- log.error("调HTTP请求出现超时:"+e.getMessage());
- buffer.append("调HTTP请求出现超时异常:"+e.getMessage());
- } catch (IOException e) {
- log.error("调HTTP请求出现IO异常:"+e.getMessage());
- buffer.append("调HTTP请求出现IO异常:"+e.getMessage());
- } catch (Exception e){
- log.error("调HTTP请求出现异常:"+e.getMessage());
- buffer.append("调HTTP请求出现异常:"+e.getMessage());
- } finally {
- if(inputStreamReader != null){
- try {
- inputStreamReader.close();
- } catch (IOException e) {
- inputStreamReader = null;
- log.error("释放inputStreamReader异常:"+e.getMessage());
- }
- }
- if(inputStream != null){
- try {
- inputStream.close();
- } catch (IOException e) {
- inputStream = null;
- log.error("释放inputStream异常:"+e.getMessage());
- }
- }
- if(out != null){
- try {
- out.close();
- } catch (IOException e) {
- out = null;
- log.error("释放out异常:"+e.getMessage());
- }
- }
- if(reader != null){
- try {
- reader.close();
- } catch (IOException e) {
- reader = null;
- log.error("释放reader异常:"+e.getMessage());
- }
- }
- if (connection != null) {
- connection.disconnect();
- }
- }
- return buffer.toString();
- }
-
- /**
- * 连接到服务器并获取数据
- * POST方式发起请求
- * @param method 请求方法,POST/PUT
- * @param urlStr 请求URL
- * @param content 请求报文
- * @param reqProperty 请求属性
- * @return
- */
- public static String sendHttpByPost(String method, String urlStr, String content, Map reqProperty) throws Exception {
- URL url = null;
- HttpURLConnection connection = null;
- StringBuffer buffer = new StringBuffer();
- BufferedReader reader = null;
- DataOutputStream out = null;
- InputStream inputStream = null;
- InputStreamReader inputStreamReader = null;
- try {
- url = new URL(urlStr);
- connection = (HttpURLConnection) url.openConnection();
- if(reqProperty != null && reqProperty.size()>0){
- Set keySet = reqProperty.keySet();
- Iterator iterator = keySet.iterator();
- String key = "";
- String value = "";
- while(iterator.hasNext()){
- key = (String)iterator.next();
- value = (String)reqProperty.get(key);
- connection.setRequestProperty(key, value);
- }
- }
- connection.setDoOutput(true);
- connection.setDoInput(true);
- connection.setRequestMethod(method);
- connection.setUseCaches(false);
- //设置超时时间为30秒
- connection.setConnectTimeout(30000);
- connection.setReadTimeout(30000);
- connection.connect();
- out = new DataOutputStream(connection.getOutputStream());
- out.write(content.getBytes("utf-8"));
- out.flush();
- out.close();
- inputStream = connection.getInputStream();
- inputStreamReader = new InputStreamReader(inputStream, "utf-8");
- reader = new BufferedReader(inputStreamReader);
- String line = "";
- while ((line = reader.readLine()) != null) {
- buffer.append(line);
- }
-
- reader.close();
- } catch (SocketTimeoutException e){
- log.error("调HTTP请求出现超时:"+e.getMessage());
- buffer.append("调HTTP请求出现超时异常:"+e.getMessage());
- throw e;
- } catch (IOException e) {
- log.error("调HTTP请求出现IO异常:"+e.getMessage());
- buffer.append("调HTTP请求出现IO异常:"+e.getMessage());
- throw e;
- } catch (Exception e){
- log.error("调HTTP请求出现异常:"+e.getMessage());
- buffer.append("调HTTP请求出现异常:"+e.getMessage());
- throw e;
- } finally {
- if(inputStreamReader != null){
- try {
- inputStreamReader.close();
- } catch (IOException e) {
- inputStreamReader = null;
- log.error("释放inputStreamReader异常:"+e.getMessage());
- }
- }
- if(inputStream != null){
- try {
- inputStream.close();
- } catch (IOException e) {
- inputStream = null;
- log.error("释放inputStream异常:"+e.getMessage());
- }
- }
- if(out != null){
- try {
- out.close();
- } catch (IOException e) {
- out = null;
- log.error("释放out异常:"+e.getMessage());
- }
- }
- if(reader != null){
- try {
- reader.close();
- } catch (IOException e) {
- reader = null;
- log.error("释放reader异常:"+e.getMessage());
- }
- }
- if (connection != null) {
- connection.disconnect();
- }
- }
- return buffer.toString();
- }
- }
|