因为实际应用中会有很多的情景需要使用他人的接口,所以记录一种自己惯用的接口访问方式。
使用apache的CloseableHttpClient进行一次POST请求的记录
记录代码如下:
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 请求工具类,用来向远程接口进行GET,POST请求
*/
public class RequestUtils {
/**
* GET请求,不做返回值,只是学习测试
* @param httpUrl 完整的请求的地址
*/
public static void callRequest(String httpUrl){
CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(httpUrl);
try {
CloseableHttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity entity = response.getEntity();
//result: 请求的字符串结果
String result = EntityUtils.toString(entity, "UTF-8");
System.out.println("result:" + result);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 向远程接口发送请求
* @param httpUrl 请求的接口地址
* @param map 请求的参数
* @return 返回封装为JSON的请求结果
*/
public static JSONObject callRequest(String httpUrl, Map<String,String> map){
String result = "";
//1.创建客户端对象,用来发送请求
CloseableHttpClient client = HttpClients.createDefault();
//2.创建POST请求,传递请求地址
HttpPost post = new HttpPost(httpUrl);
//3.参数封装,使用UrlEncodedFormEntity
Set<Map.Entry<String, String>> entries = map.entrySet();
List<NameValuePair> list = new ArrayList<>();
for (Map.Entry<String, String> entry :
map.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
try {
//4.设置POST请求传递的参数
post.setEntity(new UrlEncodedFormEntity(list));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
post.setHeader("content-type","application/x-www-form-urlencoded;charset=utf-8");
try {
//5.通过客户端对象发送设定好的POST请求
CloseableHttpResponse response = client.execute(post);
//6.如果请求成功,进行之后的操作
if(response.getStatusLine().getStatusCode() == 200){
//7.获取请求返回的实体
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
}
//8.将接口返回的结果转换成JSON字符串
JSONObject json = JSONObject.parseObject(result);
return json;
}
}
以上是进行POST,GET请求的一个主要过程,进行简单记录。
关于POST请求发送的参数的封装做另一种记录:
POST请求需要传递参数时,使用如上的UrlEncodedFormEntity的封装方法,继承于StringEntity,其中放置的参数类型只能是NameValuePair或者NameValuePair的集合;其次,UrlEncodedFormEntity对于参数的格式进行了规定,他使用“application.x-www.form-urlencoded”的参数格式。
StringEntity是另一种封装参数的方式,他继承了AbstractHttpEntity类,使用StringEntity主要是传递一个字符串类型的参数(param)和字符串的参数格式(contentType)。比起UrlEncodedFormEntity的封装形式,这一种更为灵活,可以自己指定封装的格式。封装格式也可不指定,StringEntity默认的编码格式为text/plain,ISO-8859-1。