You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
247 lines
8.7 KiB
247 lines
8.7 KiB
4 years ago
|
package com.fr.plugin.idm.sso.util;
|
||
|
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
|
||
|
import javax.net.ssl.HostnameVerifier;
|
||
|
import javax.net.ssl.HttpsURLConnection;
|
||
|
import javax.net.ssl.SSLSession;
|
||
|
import java.io.*;
|
||
|
import java.net.HttpURLConnection;
|
||
|
import java.net.URL;
|
||
|
import java.net.URLConnection;
|
||
|
import java.net.URLEncoder;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.Map;
|
||
|
import java.util.Set;
|
||
|
import java.util.stream.Collectors;
|
||
|
|
||
|
/**
|
||
|
* @author fr.open
|
||
|
* @date 2019/4/2
|
||
|
*/
|
||
|
public class HttpUtil {
|
||
|
|
||
|
private static HostnameVerifier hv = new HostnameVerifier() {
|
||
|
@Override
|
||
|
public boolean verify(String urlHostName, SSLSession session) {
|
||
|
System.out.println("Warning: URL Host: " + urlHostName + " vs. "
|
||
|
+ session.getPeerHost());
|
||
|
return true;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 发送get请求
|
||
|
*
|
||
|
* @param url
|
||
|
* @param param
|
||
|
* @param header
|
||
|
* @return
|
||
|
* @throws IOException
|
||
|
*/
|
||
|
public static String sendGet(String url, Map<String, String> param, Map<String, String> header) {
|
||
|
String result = "";
|
||
|
BufferedReader in = null;
|
||
|
String urlNameString = url;
|
||
|
try {
|
||
|
if (param != null) {
|
||
|
urlNameString += "?";
|
||
|
urlNameString += param.entrySet()
|
||
|
.stream()
|
||
|
.map(entry -> entry.getKey() + "=" + entry.getValue())
|
||
|
.collect(Collectors.joining("&"));
|
||
|
}
|
||
|
|
||
|
URL realUrl = new URL(urlNameString);
|
||
|
// 打开和URL之间的连接
|
||
|
HttpURLConnection connection;
|
||
|
if (url.startsWith("https")) {
|
||
|
trustAllHttpsCertificates();
|
||
|
HttpsURLConnection.setDefaultHostnameVerifier(hv);
|
||
|
connection = (HttpURLConnection) realUrl.openConnection();
|
||
|
} else {
|
||
|
connection = (HttpURLConnection) realUrl.openConnection();
|
||
|
}
|
||
|
//设置超时时间
|
||
|
connection.setDoInput(true);
|
||
|
connection.setRequestMethod("GET");
|
||
|
connection.setConnectTimeout(5000);
|
||
|
connection.setReadTimeout(15000);
|
||
|
// 设置通用的请求属性
|
||
|
if (header != null) {
|
||
|
Iterator<Map.Entry<String, String>> it = header.entrySet().iterator();
|
||
|
while (it.hasNext()) {
|
||
|
Map.Entry<String, String> entry = it.next();
|
||
|
System.out.println(entry.getKey() + ":::" + entry.getValue());
|
||
|
connection.setRequestProperty(entry.getKey(), entry.getValue());
|
||
|
}
|
||
|
}
|
||
|
connection.setRequestProperty("accept", "*/*");
|
||
|
connection.setRequestProperty("connection", "Keep-Alive");
|
||
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||
|
// 建立实际的连接
|
||
|
connection.connect();
|
||
|
// 定义 BufferedReader输入流来读取URL的响应,设置utf8防止中文乱码
|
||
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
|
||
|
String line;
|
||
|
while ((line = in.readLine()) != null) {
|
||
|
result += line;
|
||
|
}
|
||
|
if (in != null) {
|
||
|
in.close();
|
||
|
}
|
||
|
}catch (Exception e){
|
||
|
FineLoggerFactory.getLogger().error(e,"get url error ,url is:{},error is {}",urlNameString,e.getMessage());
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
public static String sendPost(String url,Map<String,String> header, Map<String, String> param) {
|
||
|
PrintWriter out = null;
|
||
|
BufferedReader in = null;
|
||
|
String result = StringUtils.EMPTY;
|
||
|
String res = StringUtils.EMPTY;
|
||
|
try {
|
||
|
String urlNameString = url;
|
||
|
/*if (param != null && !param.isEmpty()) {
|
||
|
urlNameString += "?";
|
||
|
urlNameString += param.entrySet()
|
||
|
.stream()
|
||
|
.map(entry -> entry.getKey() + "=" + entry.getValue())
|
||
|
.collect(Collectors.joining("&"));
|
||
|
}*/
|
||
|
|
||
|
URL realUrl = new URL(urlNameString);
|
||
|
// 打开和URL之间的连接
|
||
|
HttpURLConnection conn;
|
||
|
if (url.startsWith("https")) {
|
||
|
trustAllHttpsCertificates();
|
||
|
HttpsURLConnection.setDefaultHostnameVerifier(hv);
|
||
|
conn = (HttpURLConnection) realUrl.openConnection();
|
||
|
} else {
|
||
|
conn = (HttpURLConnection) realUrl.openConnection();
|
||
|
}
|
||
|
// 设置通用的请求属性
|
||
|
conn.setRequestProperty("accept", "*/*");
|
||
|
conn.setRequestProperty("connection", "Keep-Alive");
|
||
|
conn.setRequestProperty("user-agent",
|
||
|
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||
|
//conn.setRequestProperty("Content-Type","application/json;;charset=UTF-8");
|
||
|
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=----footfoodapplicationrequestnetwork");
|
||
|
if(header != null){
|
||
|
header.forEach((k, v) -> {
|
||
|
conn.setRequestProperty(k, v);
|
||
|
});
|
||
|
}
|
||
|
// 发送POST请求必须设置如下两行
|
||
|
conn.setDoOutput(true);
|
||
|
conn.setDoInput(true);
|
||
|
//获取请求头
|
||
|
|
||
|
// 获取URLConnection对象对应的输出流
|
||
|
out = new PrintWriter(conn.getOutputStream());
|
||
|
StringBuffer buffer = new StringBuffer();
|
||
|
param.forEach((k,v)->{
|
||
|
buffer.append("------footfoodapplicationrequestnetwork\r\n");
|
||
|
buffer.append("Content-Disposition: form-data; name=\"");
|
||
|
buffer.append(k);
|
||
|
buffer.append("\"\r\n\r\n");
|
||
|
buffer.append(v);
|
||
|
buffer.append("\r\n");
|
||
|
});
|
||
|
buffer.append("------footfoodapplicationrequestnetwork--\r\n");
|
||
|
out.print(buffer.toString());
|
||
|
/*// 发送请求参数
|
||
|
if(body != null){
|
||
|
out.print(body.toString());
|
||
|
}*/
|
||
|
// flush输出流的缓冲
|
||
|
out.flush();
|
||
|
// 定义BufferedReader输入流来读取URL的响应
|
||
|
in = new BufferedReader(
|
||
|
new InputStreamReader(conn.getInputStream()));
|
||
|
String line;
|
||
|
while ((line = in.readLine()) != null) {
|
||
|
result += line;
|
||
|
}
|
||
|
res = result;
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(),e);
|
||
|
}
|
||
|
//使用finally块来关闭输出流、输入流
|
||
|
finally{
|
||
|
try{
|
||
|
if(out!=null){
|
||
|
out.close();
|
||
|
}
|
||
|
if(in!=null){
|
||
|
in.close();
|
||
|
}
|
||
|
}
|
||
|
catch(IOException e){
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(),e);
|
||
|
}
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
private static void trustAllHttpsCertificates() throws Exception {
|
||
|
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
|
||
|
javax.net.ssl.TrustManager tm = new miTM();
|
||
|
trustAllCerts[0] = tm;
|
||
|
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL","SunJSSE");
|
||
|
sc.init(null, trustAllCerts, null);
|
||
|
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* encode url by UTF-8
|
||
|
* @param url url before encoding
|
||
|
* @return url after encoding
|
||
|
*/
|
||
|
public static String encodeUrl(String url){
|
||
|
String eurl = url;
|
||
|
try {
|
||
|
eurl = URLEncoder.encode(url,"UTF-8");
|
||
|
} catch (UnsupportedEncodingException e) {
|
||
|
}
|
||
|
return eurl;
|
||
|
}
|
||
|
|
||
|
private static class miTM implements javax.net.ssl.TrustManager,
|
||
|
javax.net.ssl.X509TrustManager {
|
||
|
@Override
|
||
|
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public boolean isServerTrusted(
|
||
|
java.security.cert.X509Certificate[] certs) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public boolean isClientTrusted(
|
||
|
java.security.cert.X509Certificate[] certs) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void checkServerTrusted(
|
||
|
java.security.cert.X509Certificate[] certs, String authType)
|
||
|
throws java.security.cert.CertificateException {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void checkClientTrusted(
|
||
|
java.security.cert.X509Certificate[] certs, String authType)
|
||
|
throws java.security.cert.CertificateException {
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|