一步一步了解http post 请求 (大白话版)。
1.创建一 个 CloseableHttpClient 对象
CloseableHttpClient client = HttpClients.createDefault();
2.创建一个httppost对象
HttpPost httpPost=new HttpPost("此处为请求url");
3.设置超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(400000).setConnectTimeout(400000).setConnectionRequestTimeout(400000).build();
4.讲 requestConfig 添加到http请求中
httpPost.setConfig(requestConfig);
5.设置http请求的请求头信息//请求头信息中一般设置 Content-Type ,在调用第三方时,秘钥、token一般也放在此处
httpPost.setHeader("key","value");
6.设置http请求的请求体信息
httpPost.setEntity("key","value");
7. 请求url获取返回信息
CloseableHttpResponse execute = closeableHttpClient.execute(httpPost);
8.解析返回code码是否为200//不为200说明请求失败
execute.getStatusLine().getStatusCode() == 200
9.获取返回信息请求体
HttpEntity he = execute.getEntity();
10.对请求体信息进行转码
String respContent = EntityUtils.toString(he,"UTF-8");