|
@@ -1,76 +1,28 @@
|
|
package cn.com.qmth.examcloud.commons.util;
|
|
package cn.com.qmth.examcloud.commons.util;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
-import java.io.InputStreamReader;
|
|
|
|
-import java.io.OutputStreamWriter;
|
|
|
|
-import java.net.HttpURLConnection;
|
|
|
|
-import java.net.URL;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 类注释
|
|
|
|
|
|
+ * httpclient
|
|
*
|
|
*
|
|
* @author WANGWEI
|
|
* @author WANGWEI
|
|
|
|
+ * @date 2019年9月17日
|
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
*/
|
|
*/
|
|
public class HttpClientUtil {
|
|
public class HttpClientUtil {
|
|
- private static final ExamCloudLog LOGGER = ExamCloudLogFactory.getLog(HttpClientUtil.class);
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 方法注释
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param url
|
|
|
|
- * @param data
|
|
|
|
- * @param timeout
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static String post(String url, String data, int timeout) {
|
|
|
|
- StringBuilder responseBuilder = null;
|
|
|
|
- BufferedReader reader = null;
|
|
|
|
- OutputStreamWriter wr = null;
|
|
|
|
-
|
|
|
|
- URL u = null;
|
|
|
|
- try {
|
|
|
|
- u = new URL(url);
|
|
|
|
- HttpURLConnection conn = (HttpURLConnection) u.openConnection();
|
|
|
|
-
|
|
|
|
- conn.setDoOutput(true);
|
|
|
|
- conn.setDoInput(true);
|
|
|
|
|
|
|
|
- conn.setRequestMethod("POST");
|
|
|
|
-
|
|
|
|
- if (null != data) {
|
|
|
|
- conn.setConnectTimeout(timeout);
|
|
|
|
- wr = new OutputStreamWriter(conn.getOutputStream());
|
|
|
|
-
|
|
|
|
- wr.write(data);
|
|
|
|
- wr.flush();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
|
- responseBuilder = new StringBuilder();
|
|
|
|
- String line = null;
|
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
|
- responseBuilder.append(line).append("\n");
|
|
|
|
- }
|
|
|
|
- return responseBuilder.toString();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(wr);
|
|
|
|
- IOUtils.closeQuietly(reader);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ private static final ExamCloudLog LOGGER = ExamCloudLogFactory.getLog(HttpClientUtil.class);
|
|
|
|
|
|
/**
|
|
/**
|
|
* 关闭流
|
|
* 关闭流
|
|
@@ -89,10 +41,32 @@ public class HttpClientUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * GET 请求
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param url
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
public static byte[] get(String url) {
|
|
public static byte[] get(String url) {
|
|
- CloseableHttpClient httpclient = HttpClientPool.getHttpClient();
|
|
|
|
|
|
+ return get(url, 1048576L);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * GET 请求
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param url
|
|
|
|
+ * @param maxByteSize
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static byte[] get(String url, long maxByteSize) {
|
|
|
|
+
|
|
|
|
+ CloseableHttpClient httpclient = HttpClientBuilder.create().build();
|
|
|
|
+
|
|
HttpGet get = new HttpGet(url);
|
|
HttpGet get = new HttpGet(url);
|
|
get.setConfig(RequestConfig.custom().setConnectTimeout(10000).build());
|
|
get.setConfig(RequestConfig.custom().setConnectTimeout(10000).build());
|
|
|
|
+
|
|
CloseableHttpResponse response = null;
|
|
CloseableHttpResponse response = null;
|
|
try {
|
|
try {
|
|
response = httpclient.execute(get);
|
|
response = httpclient.execute(get);
|
|
@@ -102,7 +76,11 @@ public class HttpClientUtil {
|
|
return byteArray;
|
|
return byteArray;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ close(response);
|
|
|
|
+ IOUtils.closeQuietly(httpclient);
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|