package org.linxidev.utils;
import java.net.URL;
import java.util.Map;
import java.util.HashMap;
import java.io.OutputStream;
import java.io.BufferedReader;
import javax.net.ssl.SSLContext;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import javax.net.ssl.TrustManager;
import com.alibaba.fastjson2.JSON;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.HttpsURLConnection;
import com.alibaba.fastjson2.JSONObject;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
public class HttpsUtils {
public static String jsonParse(JSONObject json) { return JSON.toJSONString(json); }
public static JSONObject jsonFormat(String json) { return JSONObject.parseObject(json); }
public static String paramsToUrl(String params) {
if (params == null) { return ""; }
JSONObject paramsMap = jsonFormat(params);
StringBuilder sb = new StringBuilder("?");
for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
if (sb.length() > 0) { sb.setLength(sb.length() - 1); }
return sb.toString();
}
private static void sslCert(HttpURLConnection con, boolean cert) throws NoSuchAlgorithmException, KeyManagementException {
if (con instanceof HttpsURLConnection) {
if (cert) {
SSLContext defaultSslContext = SSLContext.getDefault();
if (defaultSslContext == null) {
defaultSslContext = SSLContext.getInstance("TLS");
defaultSslContext.init(null, null, null);
}
((HttpsURLConnection) con).setSSLSocketFactory(defaultSslContext.getSocketFactory());
((HttpsURLConnection) con).setHostnameVerifier((hostname, session) -> hostname.equalsIgnoreCase(con.getURL().getHost()));
System.out.println("[\033[33mWarning\033[0m] SSL Certificate Verification Enabled");
} else {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
}
};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
((HttpsURLConnection) con).setSSLSocketFactory(sslContext.getSocketFactory());
((HttpsURLConnection) con).setHostnameVerifier((hostname, session) -> true);
System.out.println("[\033[33mWarning\033[0m] SSL Certificate Verification Disabled");
}
}
}
public static String urlSend(String url) throws Exception { return urlSend(url,"GET"); }
public static String urlSend(String url, String method) throws Exception { return urlSend(url, method,false); }
public static String urlSend(String url, String method, boolean cert) throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36");
headers.put("Accept", "*/*");
headers.put("Content-Type", "application/x-www-form-urlencoded");
return urlSend(url, method, cert, null, headers);
}
public static String urlSend(String url, String method, boolean cert,String data) throws Exception {
Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36");
headers.put("Accept", "*/*");
headers.put("Content-Type", "application/x-www-form-urlencoded");
return urlSend(url, method, cert, data, headers);
}
public static String urlSend(String url, String method, boolean cert, String data, Map<String, String> headers) throws Exception {
if ("GET".equals(method)){url = url + paramsToUrl(data);}
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
sslCert(con, cert);
con.setRequestMethod(method);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
con.setRequestProperty(entry.getKey(), entry.getValue());
}
}
if ("POST".equals(method)){
con.setDoOutput(true);
if (data != null) {
try (OutputStream os = con.getOutputStream()) {
byte[] input = data.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}catch (Exception e){
System.out.println("[\033[31m Error \033[0m] URL:" + url + " FAIL:" + e.getMessage());
return "";
}finally {
con.disconnect();
}
}
}
try {
int code = con.getResponseCode();
StringBuilder response = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader((code >= 200 && code < 300) ? con.getInputStream() : con.getErrorStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
System.out.println("[\033[32mSuccess\033[0m] Code:" + code + " Response:" + response);
return response.toString();
} catch (Exception e) {
System.out.println("[\033[31m Error \033[0m] Code:" + code + " Response:" + e.getMessage());
return "";
}
}catch (Exception e){
System.out.println("[\033[31m Error \033[0m] " + e.getMessage());
return "";
}finally {
con.disconnect();
}
}
public static void main(String[] args) {
try {
String response;
response = HttpsUtils.urlSend("http://blog.xxxx.tk");
System.out.println("HTTP GET Response: " + response);
response = HttpsUtils.urlSend("https://blog.xxxx.tk", "GET",true);
System.out.println("HTTPS GET (Cert) Response: " + response);
response = HttpsUtils.urlSend("https://blog.xxxx.tk", "GET");
System.out.println("HTTPS GET (No Cert) Response: " + response);
response = HttpsUtils.urlSend("http://blog.xxxx.tk", "POST");
System.out.println("HTTP POST Response: " + response);
response = HttpsUtils.urlSend("https://blog.xxxx.tk", "POST",true);
System.out.println("HTTPS POST (Cert) Response: " + response);
response = HttpsUtils.urlSend("https://blog.xxxx.tk", "POST");
System.out.println("HTTPS POST (No Cert) Response: " + response);
response = HttpsUtils.urlSend("http://so.studypro.club/api/search", "POST",false,"question=今天&phone=");
System.out.println("DATA Response: " + response);
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
response = HttpsUtils.urlSend("http://so.studypro.club/api/search", "POST",false, "{\"question\": \"今天\",\"phone\":\"\"}", headers);
System.out.println("JSON Response: " + response);
headers = new HashMap<>();
headers.put("Content-Type", "text/xml; charset=utf-8");
headers.put("SOAPAction", "http://WebXml.com.cn/getAreaDataSet");
response = HttpsUtils.urlSend("http://ws.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx", "POST",false,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
" <soap12:Body>\n" +
" <getAreaDataSet xmlns=\"http://WebXml.com.cn/\" />\n" +
" </soap12:Body>\n" +
"</soap12:Envelope>",headers);
System.out.println("SOAP Response: " + response);
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "LineX");
jsonObject.put("age", 18);
System.out.println(HttpsUtils.jsonParse(jsonObject));
} catch (Exception e) {
e.printStackTrace();
}
}
}