From c6037befc88e7e92e93fb928ce20078b6fe293d8 Mon Sep 17 00:00:00 2001 From: yj <Administrator@192> Date: 星期六, 07 三月 2020 12:02:24 +0800 Subject: [PATCH] 发圈倒计时 --- utils/src/main/java/com/qcloud/cmq/CMQHttp.java | 210 ++++++++++++++++++++++++++-------------------------- 1 files changed, 105 insertions(+), 105 deletions(-) diff --git a/utils/src/main/java/com/qcloud/cmq/CMQHttp.java b/utils/src/main/java/com/qcloud/cmq/CMQHttp.java index 254f7f7..6c7f555 100644 --- a/utils/src/main/java/com/qcloud/cmq/CMQHttp.java +++ b/utils/src/main/java/com/qcloud/cmq/CMQHttp.java @@ -1,105 +1,105 @@ -package com.qcloud.cmq; - -import java.io.BufferedReader; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLConnection; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLSession; - -public class CMQHttp { - private int timeout ; - private boolean isKeepAlive; - private URLConnection connection; - private String url ; - - public CMQHttp() - { - this.connection = null; - this.url = ""; - this.timeout = 10000; - this.isKeepAlive = true; - } - /* - * if we find the url is different with this.url we should new another connection - * - */ - private void newHttpConnection(String url) throws Exception - { - if(this.url != url) - { - URL realUrl = new URL(url); - if(url.toLowerCase().startsWith("https")){ - HttpsURLConnection httpsConn = (HttpsURLConnection)realUrl.openConnection(); - httpsConn.setHostnameVerifier(new HostnameVerifier(){ - public boolean verify(String hostname, SSLSession session){ - return true; - } - }); - connection = httpsConn; - } - else{ - connection = realUrl.openConnection(); - } - this.connection.setRequestProperty("Accept", "*/*"); - if(this.isKeepAlive) - this.connection.setRequestProperty("Connection", "Keep-Alive"); - this.connection.setRequestProperty("User-Agent", - "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); - - this.url = url ; - } - } - public String request(String method, String url, String req, - int userTimeout) throws Exception { - String result = ""; - BufferedReader in = null; - try{ - if (this.url != url) - this.newHttpConnection(url); - - this.connection.setConnectTimeout(timeout+userTimeout); - - if (method.equals("POST")) { - ((HttpURLConnection)this.connection).setRequestMethod("POST"); - - this.connection.setDoOutput(true); - this.connection.setDoInput(true); - DataOutputStream out = new DataOutputStream(this.connection.getOutputStream()); - out.writeBytes(req); - out.flush(); - out.close(); - } - - this.connection.connect(); - int status = ((HttpURLConnection)this.connection).getResponseCode(); - if(status != 200) - throw new CMQServerException(status); - - in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8")); - - String line; - while ((line = in.readLine()) != null) { - result += line; - } - }catch(Exception e){ - throw e; - }finally{ - try { - if (in != null) - in.close(); - } catch (Exception e2) { - throw e2; - } - } - - return result; - } -} +package com.qcloud.cmq; + +import java.io.BufferedReader; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLSession; + +public class CMQHttp { + private int timeout ; + private boolean isKeepAlive; + private URLConnection connection; + private String url ; + + public CMQHttp() + { + this.connection = null; + this.url = ""; + this.timeout = 10000; + this.isKeepAlive = true; + } + /* + * if we find the url is different with this.url we should new another connection + * + */ + private void newHttpConnection(String url) throws Exception + { + if(this.url != url) + { + URL realUrl = new URL(url); + if(url.toLowerCase().startsWith("https")){ + HttpsURLConnection httpsConn = (HttpsURLConnection)realUrl.openConnection(); + httpsConn.setHostnameVerifier(new HostnameVerifier(){ + public boolean verify(String hostname, SSLSession session){ + return true; + } + }); + connection = httpsConn; + } + else{ + connection = realUrl.openConnection(); + } + this.connection.setRequestProperty("Accept", "*/*"); + if(this.isKeepAlive) + this.connection.setRequestProperty("Connection", "Keep-Alive"); + this.connection.setRequestProperty("User-Agent", + "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + + this.url = url ; + } + } + public String request(String method, String url, String req, + int userTimeout) throws Exception { + String result = ""; + BufferedReader in = null; + try{ + if (this.url != url) + this.newHttpConnection(url); + + this.connection.setConnectTimeout(timeout+userTimeout); + + if (method.equals("POST")) { + ((HttpURLConnection)this.connection).setRequestMethod("POST"); + + this.connection.setDoOutput(true); + this.connection.setDoInput(true); + DataOutputStream out = new DataOutputStream(this.connection.getOutputStream()); + out.writeBytes(req); + out.flush(); + out.close(); + } + + this.connection.connect(); + int status = ((HttpURLConnection)this.connection).getResponseCode(); + if(status != 200) + throw new CMQServerException(status); + + in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8")); + + String line; + while ((line = in.readLine()) != null) { + result += line; + } + }catch(Exception e){ + throw e; + }finally{ + try { + if (in != null) + in.close(); + } catch (Exception e2) { + throw e2; + } + } + + return result; + } +} -- Gitblit v1.8.0