Send HTTP
6934 ワード
huotoolを使う
cn.hutool
hutool-all
4.5.16
cn.hutol.http.HttpRequestオブジェクトを使用してデフォルトでSSL認証をスキップします。 public void test(){
JSONObject requestJson = new JSONObject();
requestJson.put("name", "zhang3");
requestJson.put("age", 10);
String requestParam = requestJson.toJSONString();
HttpResponse responseObj = HttpRequest.post("https://www.baidu.com").body(requestParam).execute();
}
apphe httpclientを使う4.5.2
org.apache.httpcomponents
httpclient
4.5.2
compile
org.apache.httpcomponents
httpcore
commons-logging
commons-logging
commons-codec
commons-codec
HTTP Postの送信要求はSSL認証をスキップする:public void test(){
JSONObject requestJson = new JSONObject();
requestJson.put("name", "zhang3");
requestJson.put("age", 10);
String requestParam = requestJson.toJSONString();
String response = sendPostRequest("https://www.baidu.con", requestParam, "application/json; charset=UTF-8");
}
public static String sendPostRequest(String url, String params, String contentType){
StringBuilder response = new StringBuilder();
try {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, (x509CertChain, authType) -> true)
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setSSLContext(sslContext)
.setConnectionManager(
new PoolingHttpClientConnectionManager(
RegistryBuilder.create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslContext,
NoopHostnameVerifier.INSTANCE))
.build()
))
.build();
HttpPost httpPost = new HttpPost(url);
// //
// List list = new ArrayList<>();
// list.add(new BasicNameValuePair("j_username", "admin"));
// list.add(new BasicNameValuePair("j_password", "admin"));
// //url
// UrlEncodedFormEntity params = new UrlEncodedFormEntity(list,StandardCharsets.UTF_8);
StringEntity stringEntity = new StringEntity(params, StandardCharsets.UTF_8);
httpPost.setHeader("Content-Type", contentType);
// httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(stringEntity);
logger.info("POST : {}", httpPost.getURI());
logger.info("POST : {}", EntityUtils.toString(stringEntity));
//
try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
HttpEntity entity = httpResponse.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
logger.info(" :-------------------------------------------------------");
// logger.info(response.toString());
}
} catch( IOException e){
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return response.toString();
}
HTTP Postの送信要求はSSL認証をスキップしない:public void test(){
JSONObject requestJson = new JSONObject();
requestJson.put("name", "zhang3");
requestJson.put("age", 10);
String response = sendPostRequest("https://www.baidu.con", requestParam, "application/json; charset=UTF-8");
}
public static String sendPostRequest(String url, String params, String contentType){
StringBuilder response = new StringBuilder();
try (CloseableHttpClient httpClient = HttpClients.createDefault()){
HttpPost httpPost = new HttpPost(url);
// //
// List list = new ArrayList<>();
// list.add(new BasicNameValuePair("j_username", "admin"));
// list.add(new BasicNameValuePair("j_password", "admin"));
// //url
// UrlEncodedFormEntity params = new UrlEncodedFormEntity(list,StandardCharsets.UTF_8);
StringEntity stringEntity = new StringEntity(params, StandardCharsets.UTF_8);
httpPost.setHeader("Content-Type", contentType);
// httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(stringEntity);
logger.info("POST : {}", httpPost.getURI());
logger.info("POST : {}", EntityUtils.toString(stringEntity));
//
try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
HttpEntity entity = httpResponse.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
logger.info(" :-------------------------------------------------------");
// logger.info(response.toString());
}
} catch( IOException e){
e.printStackTrace();
}
return response.toString();
}