本次调用遵循服务器端指定的RESTful风格。当前博客只接受post请求
首先引入jar包依赖,我这里将代码抠出来了。只是简介。
<!-- httpclient start -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
<!-- httpclient end -->
<httpclient.version>4.5.3</httpclient.version>
<httpcore.version>4.4.6</httpcore.version>
下面是http工具类
package org.neris.CDExchange.util;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.neris.CDExchange.entity.HttpResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* http工具类
* @author SiQiangMing 2020年4月22日 下午2:27:16
*/
public class HttpUtils {
private static Logger log = LoggerFactory.getLogger(HttpUtils.class);
/**
* HttpPost请求
* @author SiQiangMing 2020年4月22日 下午4:48:27
* @param uri 请求地址
* @param json json格式的参数
* @return
*/
public static HttpResponseResult post(String uri, String json){
log.info(uri+ ": post请求 : " + json);
//生成post请求
HttpPost httpPost = new HttpPost(uri);
try {
//设置参数编码格式,如果不用Charset.forName("UTF-8"),服务端中文乱码
StringEntity entity = new StringEntity(json, Charset.forName("UTF-8"));
//设置参数格式为json
entity.setContentType("application/json");
//put请求的参数
httpPost.setEntity(entity);
return result(post(httpPost));
} catch (Exception e) {
log.error("post请求错误");
e.printStackTrace();
} finally {
//释放连接
httpPost.releaseConnection();
}
return null;
}
/**
* 请求结果
* @author SiQiangMing 2020年4月23日 上午10:01:59
* @param httpResponse 请求返回对象
* @return 请求结果
*/
private static HttpResponseResult result(HttpResponse httpResponse) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
String result = null;
if(statusCode == 200){
result = getResult(httpResponse);
}
log.info(statusCode + ": post请求返回对象:" + result);
HttpResponseResult httpResponseResult = new HttpResponseResult(statusCode, result);
return httpResponseResult;
}
/**
* 发送post请求,得到post请求反回对象
* @author SiQiangMing 2020年4月23日 上午9:35:48
* @param httpPost post请求实例
* @return 请求返回对象
* @throws IOException
* @throws ClientProtocolException
*/
private static HttpResponse post(HttpPost httpPost)
throws IOException, ClientProtocolException {
return HttpClientBuilder.create().build().execute(httpPost);
}
/**
* 获取请求返回结果内容
* @author SiQiangMing 2020年4月22日 下午4:50:06
* @param httpResponse
* @return
*/
public static String getResult(HttpResponse httpResponse){
//对象中取出实体
HttpEntity httpEntity = httpResponse.getEntity();
try {
//将实体转为字符串
return EntityUtils.toString(httpEntity);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
其中注意的是中文乱码问题。
//设置参数编码格式,如果不用Charset.forName("UTF-8"),服务端中文乱码
StringEntity entity = new StringEntity(json, Charset.forName("UTF-8"));
其实本篇博文技术没有什么难点。还是分享一下。这里是client。