String str = "";
//超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(6000).setConnectTimeout(6000).build();
HttpClientBuilder httpClientBuilder = HttpClients.custom();
HttpPost httpPost = new HttpPost("http://123456789:123/sjfkd/dsaf");
httpPost.setConfig(requestConfig);
//封装参数
StringEntity stringEntity = new StringEntity(str,"UTF-8");
httpPost.setEntity(stringEntity);
CloseableHttpClient httpClient = httpClientBuilder.build();
try{
CloseableHttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode== HttpStatus.SC_OK){
HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString( httpEntity,"UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
}
maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>