|
|
|
|
@ -36,8 +36,7 @@ import java.util.List;
|
|
|
|
|
*
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
public class HttpUtils
|
|
|
|
|
{
|
|
|
|
|
public class HttpUtils {
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -47,8 +46,7 @@ public class HttpUtils
|
|
|
|
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
|
|
*/
|
|
|
|
|
public static String sendGet(String url, String param)
|
|
|
|
|
{
|
|
|
|
|
public static String sendGet(String url, String param) {
|
|
|
|
|
return sendGet(url, param, "UTF-8");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -60,12 +58,10 @@ public class HttpUtils
|
|
|
|
|
* @param contentType 编码类型
|
|
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
|
|
*/
|
|
|
|
|
public static String sendGet(String url, String param, String contentType)
|
|
|
|
|
{
|
|
|
|
|
public static String sendGet(String url, String param, String contentType) {
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
BufferedReader in = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
String urlNameString = url + "?" + param;
|
|
|
|
|
log.info("sendGet - {}", urlNameString);
|
|
|
|
|
URL realUrl = new URL(urlNameString);
|
|
|
|
|
@ -76,39 +72,24 @@ public class HttpUtils
|
|
|
|
|
connection.connect();
|
|
|
|
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = in.readLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
|
result.append(line);
|
|
|
|
|
}
|
|
|
|
|
log.info("recv - {}", result);
|
|
|
|
|
}
|
|
|
|
|
catch (ConnectException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (ConnectException e) {
|
|
|
|
|
log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
catch (SocketTimeoutException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (SocketTimeoutException e) {
|
|
|
|
|
log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (in != null)
|
|
|
|
|
{
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
if (in != null) {
|
|
|
|
|
in.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -122,13 +103,11 @@ public class HttpUtils
|
|
|
|
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
|
|
*/
|
|
|
|
|
public static String sendPost(String url, String param)
|
|
|
|
|
{
|
|
|
|
|
public static String sendPost(String url, String param) {
|
|
|
|
|
PrintWriter out = null;
|
|
|
|
|
BufferedReader in = null;
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
String urlNameString = url;
|
|
|
|
|
log.info("sendPost - {}", urlNameString);
|
|
|
|
|
URL realUrl = new URL(urlNameString);
|
|
|
|
|
@ -145,55 +124,37 @@ public class HttpUtils
|
|
|
|
|
out.flush();
|
|
|
|
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = in.readLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
|
result.append(line);
|
|
|
|
|
}
|
|
|
|
|
log.info("recv - {}", result);
|
|
|
|
|
}
|
|
|
|
|
catch (ConnectException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (ConnectException e) {
|
|
|
|
|
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
catch (SocketTimeoutException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (SocketTimeoutException e) {
|
|
|
|
|
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (out != null)
|
|
|
|
|
{
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
if (out != null) {
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
|
|
|
|
if (in != null)
|
|
|
|
|
{
|
|
|
|
|
if (in != null) {
|
|
|
|
|
in.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String sendSSLPost(String url, String param)
|
|
|
|
|
{
|
|
|
|
|
public static String sendSSLPost(String url, String param) {
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
String urlNameString = url + "?" + param;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
log.info("sendSSLPost - {}", urlNameString);
|
|
|
|
|
SSLContext sc = SSLContext.getInstance("SSL");
|
|
|
|
|
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
|
|
|
|
|
@ -213,63 +174,48 @@ public class HttpUtils
|
|
|
|
|
InputStream is = conn.getInputStream();
|
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
|
|
|
|
String ret = "";
|
|
|
|
|
while ((ret = br.readLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
if (ret != null && !"".equals(ret.trim()))
|
|
|
|
|
{
|
|
|
|
|
while ((ret = br.readLine()) != null) {
|
|
|
|
|
if (ret != null && !"".equals(ret.trim())) {
|
|
|
|
|
result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.info("recv - {}", result);
|
|
|
|
|
conn.disconnect();
|
|
|
|
|
br.close();
|
|
|
|
|
}
|
|
|
|
|
catch (ConnectException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (ConnectException e) {
|
|
|
|
|
log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
catch (SocketTimeoutException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (SocketTimeoutException e) {
|
|
|
|
|
log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e)
|
|
|
|
|
{
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e);
|
|
|
|
|
}
|
|
|
|
|
return result.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class TrustAnyTrustManager implements X509TrustManager
|
|
|
|
|
{
|
|
|
|
|
private static class TrustAnyTrustManager implements X509TrustManager {
|
|
|
|
|
@Override
|
|
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
|
|
|
|
{
|
|
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] chain, String authType)
|
|
|
|
|
{
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] chain, String authType) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public X509Certificate[] getAcceptedIssuers()
|
|
|
|
|
{
|
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
|
return new X509Certificate[]{};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class TrustAnyHostnameVerifier implements HostnameVerifier
|
|
|
|
|
{
|
|
|
|
|
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean verify(String hostname, SSLSession session)
|
|
|
|
|
{
|
|
|
|
|
public boolean verify(String hostname, SSLSession session) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CloseableHttpClient createSSLClientDefault() {
|
|
|
|
|
try {
|
|
|
|
|
//使用 loadTrustMaterial() 方法实现一个信任策略,信任所有证书
|
|
|
|
|
@ -317,6 +263,7 @@ public class HttpUtils
|
|
|
|
|
}
|
|
|
|
|
return httpPost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static HttpGet getHttpGet(String url, List<BasicNameValuePair> list) throws UnsupportedEncodingException {
|
|
|
|
|
// 创建 HttpGet 请求
|
|
|
|
|
HttpGet httpGet = new HttpGet(url);
|
|
|
|
|
@ -337,6 +284,7 @@ public class HttpUtils
|
|
|
|
|
}
|
|
|
|
|
return httpGet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean testNetworkConnected(String url) {
|
|
|
|
|
// 创建 HttpClient 客户端
|
|
|
|
|
CloseableHttpClient httpClient = HttpUtils.createSSLClientDefault();
|
|
|
|
|
@ -355,6 +303,7 @@ public class HttpUtils
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getCookies(CloseableHttpResponse httpResponse) {
|
|
|
|
|
Header str = httpResponse.getHeaders("Set-Cookie")[0];
|
|
|
|
|
return str.getValue().split(";")[0];
|
|
|
|
|
|