httpclientローカルインタフェースをテストする
3331 ワード
1.postリクエストインタフェースの送信
/**
* Post
*
* @param httpPost
* @return
*/
private static String sendHttpPost(HttpPost httpPost) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try {
// httpClient .
httpClient = HttpClients.custom()
.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
.setDefaultRequestConfig(requestConfig).build();
//
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// ,
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}
2.テストを行う
@Test
public void postTest() throws IOException {
String path = this.getClass().getResource("/").getPath();
//findLawCaceList.json
File file = new File(path + "/com/webapp/api/test/findLawCaceList.json");
String param = FileUtils.readFileToString(file, "UTF-8");
String digest = Encrypt.getSHA256(param);
HttpPost post = new HttpPost("http://localhost: ");
post.setEntity(new StringEntity(param, "UTF-8"));
post.setHeader("digest", digest);
String result = sendHttpPost(post);
System.out.println(result);
}