代码下载
加载需要的jar
<!-- 解决编译时,报程序包不存在的错误 -->
<dependency>
<groupId></groupId>
<artifactId>-api</artifactId>
<version>3.1.0</version>
<!-- 只在编译和测试的时候用 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- /artifact//spring-boot-starter-web -->
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.</version>
</dependency>
代码
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .X509Certificate;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import org.;
import org.;
import ;
import ;
import ;
import ;
import ;
import ;
public class HttpUtil {
private static final Logger logger = ();
public static String readJSONString(HttpServletRequest request) {
String method = ();
if (method == "GET") {
return ();
} else {
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = ();
while ((line = ()) != null) {
(line);
}
} catch (Exception e) {
(());
}
return ();
}
}
public static JSONObject readJSONParam(HttpServletRequest request) {
String method = ();
if (method == "GET") {
return (());
} else {
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = ();
while ((line = ()) != null) {
(line);
}
} catch (Exception e) {
(());
}
return (());
}
}
public static ResponseEntity<String> GetResponseEntity(String result) {
HttpHeaders responseHeaders = new HttpHeaders();
MediaType mediaType = new MediaType("text", "html", ("UTF-8"));
(mediaType);
return new ResponseEntity<String>(result, responseHeaders, );
}
/**
* 获得一个忽略证书的HttpClient
*
* @return
* @throws Exception
*/
public static HttpClient getCertificateValidationIgnoredHttpClient() throws Exception {
SSLContext sslContext = ()
// 忽略掉对证书的校验
.loadTrustMaterial(new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
CloseableHttpClient client = ().setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
return client;
}
public static JSONObject doGetRequest(String requestUrl) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
// http协议传输
HttpURLConnection httpUrlConn = (HttpURLConnection) ();
(true);
(true);
(false);
// 设置请求方式(GET/POST)
("GET");
();
// 将返回的输入流转换成字符串
InputStream inputStream = ();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = ()) != null) {
(str);
}
();
();
// 释放资源
();
inputStream = null;
();
jsonObject = (());
} catch (Exception e) {
();
}
return jsonObject;
}
/**
* post请求
*
* @param url
* @param json
* @return
*/
public static JSONObject doPostRequest(String requestUrl, JSONObject json,JSONObject header) {
CloseableHttpClient client = ().build();
HttpPost post = new HttpPost(requestUrl);
JSONObject response = null;
try {
// ContentType contentType =
// (ContentType.DEFAULT_TEXT,Charsets.UTF_8);
StringEntity s = new StringEntity((), ContentType.APPLICATION_JSON);
("UTF-8");
("application/json");// 发送json数据需要设置contentType
for (String key: ()){
(key,(key));
}
(s);
HttpResponse res = (post);
if (().getStatusCode() == .SC_OK) {
HttpEntity entity = ();
String result = (());// 返回json格式:
response = (result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
/**
* 返回byte[] 字节数据 post 请求
*
* @param requestUrl
* @param json
* @return
*/
public static byte[] doPostRequestByte(String requestUrl, JSONObject json) throws Exception {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(requestUrl);
byte[] result = null;
try {
("数据 post 请求");
// ContentType contentType =
// (ContentType.DEFAULT_TEXT,Charsets.UTF_8);
StringEntity s = new StringEntity((), ContentType.APPLICATION_JSON);
("UTF-8");
("application/json");// 发送json数据需要设置contentType
(s);
("数据 post 请求1");
HttpResponse res = (post);
("结果:" + ().getStatusCode());
("结果:" + res);
if (().getStatusCode() == .SC_OK) {
HttpEntity entity = ();
result = (());// 返回图片二进制格式:
}
} catch (Exception e) {
("post错误" + e);
throw new RuntimeException(e);
}
("fanhui", result);
return result;
}
}