Skip to content

Commit

Permalink
feat(net): 添加 HttpTool 类并实现 POST 请求功能
Browse files Browse the repository at this point in the history
- 新增 HttpTool 类,提供 HTTP POST 请求的工具方法
- 实现了对 HTTPS 的支持,并添加了信任所有主机的功能
- 添加了请求头和请求体的处理
- 编写了单元测试用例,验证 HTTP 请求功能
  • Loading branch information
xuxueli committed Dec 21, 2024
1 parent e2e0326 commit f9e8e4e
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 1 deletion.
171 changes: 171 additions & 0 deletions src/main/java/com/xxl/tool/net/HttpTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package com.xxl.tool.net;

import com.xxl.tool.core.MapTool;
import com.xxl.tool.core.StringTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;

/**
* @author xuxueli 2018-11-25 00:55:31
*/
public class HttpTool {
private static Logger logger = LoggerFactory.getLogger(HttpTool.class);


// trust-https start
private static void trustAllHosts(HttpsURLConnection connection) {
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory newFactory = sc.getSocketFactory();

connection.setSSLSocketFactory(newFactory);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
connection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
}
private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
}};
// trust-https end


// ---------------------- tool ----------------------

/**
* post
*
* @param url
* @param requestBody
* @param timeout
* @return
*/
public static String postBody(String url,
String requestBody,
int timeout) {
return postBody(url, requestBody, null, timeout);
}

/**
* post
*
* @param url
* @param requestBody
* @param headers
* @param timeout
* @return
*/
public static String postBody(String url,
String requestBody,
Map<String, String> headers,
int timeout) {

HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
try {
// connection
URL realUrl = new URL(url);
connection = (HttpURLConnection) realUrl.openConnection();

// trust-https
boolean useHttps = url.startsWith("https");
if (useHttps) {
HttpsURLConnection https = (HttpsURLConnection) connection;
trustAllHosts(https);
}

// connection setting
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setReadTimeout(timeout * 1000);
connection.setConnectTimeout(3 * 1000);
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");

// header setting
if(MapTool.isNotEmpty(headers)){
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
}

// do connection
connection.connect();

// write requestBody
if (StringTool.isNotBlank(requestBody)) {
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(requestBody.getBytes("UTF-8"));
dataOutputStream.flush();
dataOutputStream.close();
}

/*byte[] requestBodyBytes = requestBody.getBytes("UTF-8");
connection.setRequestProperty("Content-Length", String.valueOf(requestBodyBytes.length));
OutputStream outwritestream = connection.getOutputStream();
outwritestream.write(requestBodyBytes);
outwritestream.flush();
outwritestream.close();*/

// valid StatusCode
int statusCode = connection.getResponseCode();
if (statusCode != 200) {
throw new RuntimeException("StatusCode("+ statusCode +") invalid. for url : " + url);
}

// result
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuilder result = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
String responseBody = result.toString();

// parse returnT
return responseBody;
} catch (Exception e) {
throw new RuntimeException("Http Request Error ("+ e.getMessage() +"). for url : " + url);
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (Exception e2) {
logger.error(e2.getMessage(), e2);
}
}
}

}
4 changes: 3 additions & 1 deletion src/test/java/com/xxl/tool/test/json/BasicJsonToolTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.xxl.tool.test.json;

import com.xxl.tool.json.BasicJsonTool;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class BasicJsonToolTest {

public static void main(String[] args) {
@Test
public void test() {
Map<String, Object> result = new HashMap<>();
result.put("int", 200);
result.put("str", "success");
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/xxl/tool/test/net/HttpToolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.xxl.tool.test.net;

import com.xxl.tool.net.HttpTool;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpToolTest {
private static final Logger logger = LoggerFactory.getLogger(HttpToolTest.class);

@Test
public void test() {
String resp = HttpTool.postBody("http://www.baidu.com/", "hello world", 3000);
logger.info(resp);
}

}

0 comments on commit f9e8e4e

Please sign in to comment.