|
|
@@ -1,5 +1,6 @@
|
|
|
package com.railway.common.utils.http;
|
|
|
|
|
|
+import com.google.common.primitives.Bytes;
|
|
|
import com.railway.common.constant.Constants;
|
|
|
import com.railway.common.utils.StringUtils;
|
|
|
import java.io.BufferedReader;
|
|
|
@@ -14,6 +15,8 @@ import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
@@ -85,8 +88,12 @@ public class HttpUtils {
|
|
|
* @param url 发送请求的 URL
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
*/
|
|
|
- public static InputStream sendGetStream(String url) {
|
|
|
- return sendGetStream(url, null);
|
|
|
+ public static byte [] sendGetStream(String url) {
|
|
|
+ InputStream in = sendGetStream(url, null);
|
|
|
+ if(null == in){
|
|
|
+ return new byte[0];
|
|
|
+ }
|
|
|
+ return inputStream2ByteArray(in);
|
|
|
}
|
|
|
/**
|
|
|
* 向指定 URL 发送GET方法的请求
|
|
|
@@ -243,6 +250,28 @@ public class HttpUtils {
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将输入流中的数据写入字节数组
|
|
|
+ */
|
|
|
+ public static byte[] inputStream2ByteArray(InputStream in) {
|
|
|
+ List<Byte> byteList = new ArrayList<>();
|
|
|
+ byte[] bytes = new byte[2048];
|
|
|
+ try {
|
|
|
+ while (in.read(bytes) != -1) {
|
|
|
+ byteList.addAll(Bytes.asList(bytes));
|
|
|
+ }
|
|
|
+ }catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (Exception e2) {
|
|
|
+ e2.getStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Bytes.toArray(byteList);
|
|
|
+ }
|
|
|
+
|
|
|
private static class TrustAnyTrustManager implements X509TrustManager {
|
|
|
|
|
|
@Override
|