首先写一个基础方法 send ,将header信息填写进来
package com.boc.web;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;
import com.sun.el.parser.ParseException;
public class PostSend {
public static String send(String url, JSONObject jsonObject, String encoding) throws ParseException, IOException {
String body = "";
//创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
//创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
//装填参数
StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
//设置参数到请求对象中
httpPost.setEntity(s);
System.out.println("请求地址:" + url);
// System.out.println("请求参数:"+nvps.toString());
//设置header信息
//指定报文头【Content-type】、【User-Agent】
// httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
httpPost.addHeader("clientId","****************");
httpPost.addHeader("clientSecret","****************");
httpPost.addHeader("grantType","***************");
//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
//获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
//按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, encoding);
}
EntityUtils.consume(entity);
//释放链接
response.close();
return body;
}
}
然后就可以在create中调用该方法了
@ResponseBody
@GetMapping("/create")
public String create(@RequestParam("poolname") String poolname, @RequestParam("phonenum") String phonenum)
throws ParseException {
//list格式的"phones"字段封装成json数组:enclosure
String data = null;
ArrayList<HashMap<String, String>> enclosureList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> tmpHash = new HashMap<String, String>();
tmpHash.put("phone", phonenum);
enclosureList.add(tmpHash);
JSONArray enclosure = JSONArray.parseArray(JSON.toJSONString(enclosureList));
//创建号码池URL
String url = "https://*****************************";
//Json对象存储访问参数
JSONObject jsonParam = new JSONObject();
jsonParam.put("name", poolname);
jsonParam.put("callCondition", "voice_upload");
jsonParam.put("phoneDecryption", "asccode");
jsonParam.put("phones", enclosure);
try {
//调用post请求方式
data = PostSend.send(url, jsonParam, "UTF-8");
}
catch (IOException e) {
e.printStackTrace();
}
return data;
}
接下来详解一下create方法,主要分成三大部分,下面逆序讲解
{
"name":"Test1",
"callCondition":"voice_upload",
"phoneDecryption":"asccode",
"phones":[
{
"phone":"188000000"
}
]
}
因此需要将数据都存储成json对象。
//Json对象存储访问参数
JSONObject jsonParam = new JSONObject();
jsonParam.put("name", poolname);
jsonParam.put("callCondition", "voice_upload");
jsonParam.put("phoneDecryption", "asccode");
jsonParam.put("phones", enclosure);
jsonParam.put 要求输入是String类型的键值对,如“callCondition”, “voice_upload”、“phoneDecryption”, “asccode”。而 poolname 是接收的前端 index.html 传输过来的字符,因此需要在方法中填写参数:
public String create(@RequestParam("poolname") String poolname, @RequestParam("phonenum") String phonenum)
throws ParseException {
同理,phonenum 也是同样利用前端传递获得的,只不过与 poolname 略有不同,见下
"phones":[
{
"phone":"188000000"
}
]
//list格式的"phones"字段封装成json数组:enclosure
String data = null;
ArrayList<HashMap<String, String>> enclosureList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> tmpHash = new HashMap<String, String>();
tmpHash.put("phone", phonenum);
enclosureList.add(tmpHash);
JSONArray enclosure = JSONArray.parseArray(JSON.toJSONString(enclosureList));
data = PostSend.send(url, jsonParam, "UTF-8");
首先写一个基础方法 send ,将header信息填写进来
package com.boc.web;
import java.util.Arrays;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class GetSend {
// ************ GET ***************
/**
* Spring Boot Get请求
*/
public static ResponseEntity<String> getsend(String url) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.add("clientId", "*****************");
headers.add("clientSecret", "**********************");
headers.add("grantType", "************************");
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
return result;
}
}
然后就可以在 taskprocess 中调用该方法了
@ResponseBody
@RequestMapping("/taskprocess")
public ResponseEntity<String> taskprocess(@RequestParam("taskid") String taskid)
throws ParseException {
ResponseEntity<String> data = null;
//外呼进度查询URL
String url = "https://robot.wewecall.com/callout-adapter/adapter/tasks/process/" + taskid;
//调用get请求方式
data = GetSend.getsend(url);
return data;
}
与post相同的原理, taskid 通过前端传入获取。
备注:
pox.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.boc</groupId>
<artifactId>start</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>start</name>
<description>Start project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.69</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<!--Alijson插件-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>
<!--集成mysql数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version><!--$NO-MVN-MAN-VER$-->
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--lombok注解简化代码-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>