HttpClient调用后台接口(用CloseableHttpClient发送get/post请求)
前言
在没有页面的情况下来获取接口返回的数据(一般都是为JSON),我们可以通过一些工具模拟HTTP请求
服务端模拟HTTP请求
通过JAVA代码进行HTTP请求的发送
引入依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
代码
public class HttpClientUtil {
//参数加在页面的get请求,用?和&衔接参数
public static void httpGet1() throws IOException, URISyntaxException {
//建立连接请求
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建post请求
HttpGet httpGet = new HttpGet("http://10.143.88.87/sap/get_bom_all?modelName=A90AI00B2-T00051&plant=WEAS");
//发送请求
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity!=null){
System.out.println("接收到的响应信息:--------"+ EntityUtils.toString(entity,"UTF-8"));
}
//资源释放
response.close();
httpClient.close();
}
//参数加在页面的get请求,用BasicNameValuePair接参数,前面是参数,后面是值。
public static void httpGet2() throws IOException, URISyntaxException {
//get请求
URIBuilder uri = new URIBuilder("http://10.143.88.87/sap/get_bom_all");
//get请求带参数
List<NameValuePair> list = new LinkedList<>();
BasicNameValuePair param1 = new BasicNameValuePair("modelName", "A90AI00B2-T00051");
BasicNameValuePair param2 = new BasicNameValuePair("plant", "WEAS");
list.add(param1);
list.add(param2);
uri.setParameters(list);
HttpGet httpGet = new HttpGet(uri.build());
//建立连接请求
CloseableHttpClient httpClient = HttpClients.createDefault();
//发送请求
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("接收到的响应信息:--------" + EntityUtils.toString(entity, "UTF-8"));
}
//资源释放
response.close();
httpClient.close();
}
//post请求,后台接收的参数是application/x-www-form-urlencoded是浏览器默认的编码格式。对于Get请求,是将参数转换?key=value&key=value格式,连接到url后,如果是POST,则x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…)
public static void httpPost1() throws IOException {
//建立连接请求
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建post请求
HttpPost httpPost = new HttpPost("http://10.83.16.208/AegisWebAPIV02/API/Aegist2/WebSerAegis101V01");
String json = "={ "+
" \"CmdNo\":\"31105\",\n" +
" \"account\":\"\",\n" +
" \"password\":\"\",\n" +
" \"tmpdate\":\"\",\n" +
" \"ProcID\":\"\",\n" +
"\"d1\":\"{\\\"Table1\\\":[{\\\"SysNo\\\":\\\"\\\",\\\"AegType\\\":\\\"103\\\",\\\"ID\\\":\\\"\\\",\\\"APassword\\\":\\\"\\\",\\\"SetDateTime\\\":\\\"\\\",\\\"AegPS\\\":\\\"\\\",\\\"APPDevice\\\":\\\"\\\",\\\"R1\\\":\\\"11210136459\\\",\\\"R2\\\":\\\"90AI0052-M00160\\\",\\\"R3\\\":\\\"R3\\\",\\\"R4\\\":\\\"R4\\\",\\\"R5\\\":\\\"R5\\\",\\\"R6\\\":\\\"R6\\\",\\\"R7\\\":\\\"R7\\\",\\\"R8\\\":\\\"R8\\\",\\\"R9\\\":\\\"R9\\\",\\\"R10\\\":\\\"R10\\\"}]}\", \"d2\":\"TEST\",\n" +
" \"d3\":\"\",\n" +
" \"d4\":\"\",\n" +
" \"d5\":\"\"\n" +
"}";
StringEntity requestEntity = new StringEntity(json,"utf-8");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
//讲实体封装到请求当中
httpPost.setEntity(requestEntity);
//发送请求
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity!=null){
System.out.println("接收到的响应信息:--------"+ EntityUtils.toString(entity,"UTF-8"));
}
//资源释放
response.close();
httpClient.close();
}
//post请求,后台接收的参数是"application/json;charset=utf-8"
public static void httpPost2() throws IOException {
//建立连接请求
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建post请求
HttpPost httpPost = new HttpPost("http://10.143.88.86:8099/sys/login2");
JSONObject jsonObject = new JSONObject();
jsonObject.put("UserCode","F2848001");
jsonObject.put("Password","abcd1234");
jsonObject.put("StationID","SOCSCREW11");
jsonObject.put("Lang","zh_tw");
jsonObject.put("SiteCode","S001");
jsonObject.put("BUCode","B201");
StringEntity requestEntity = new StringEntity(jsonObject.toString(),"utf-8");
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
//讲实体封装到请求当中
httpPost.setEntity(requestEntity);
//发送请求
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity!=null){
System.out.println("接收到的响应信息:--------"+ EntityUtils.toString(entity,"UTF-8"));
}
//资源释放
response.close();
httpClient.close();
}
public static void main(String[] args) throws IOException, URISyntaxException {
httpPost2();
}
}
注:
如果PostMethod提交的是中文字符,需要加上相应的编码格式: post.setRequestHeader(“Content-Type”,“application/x-www-form-urlencoded;charset=utf-8”);
如果GetMethod提交的参数有中文字符,需要先转换成utf-8格式:String param = “enterpriseName=”+ URLEncoder.encode(“中国移动”, “utf-8”); HttpUtil.doGet(param, url);
maven依赖说明
commons-httpclient 是 apache-commons 项目下的一个子项目,后来被 HttpComponents 取代,后者提供了更好的性能和更大的灵活性。
commons-httpclient的GAV地址为
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
其最新版本为3.1,且已经不再更新;
HttpComponents的GAV地址为
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
post SAP-Garson
原文链接:https://blog.csdn.net/qq_34246965/article/details/123782848文章来自于网络,如果侵犯了您的权益,请联系站长删除!