-------------------------------------A:服务器发出请求-------------------------------------
@RequestMapping("/portal")
@ResponseBody
public String getTest(@RequestBody PortalVo pvo){
String interfacet = pvo.getInterfacet();
String data = pvo.getData();
String url = "http://127.0.0.1:10082/security/initDataByNums";
Map<String,Object> maps = ThirdPartyInterfaceUtils.getExternalInfo(url,data);
return maps.toString();
}
-------------------------------------Postman post请求-------------------------------------
{
"interfacename":"1",
"data":"{'aa':'111','aa2':'111','aa3':'111'}"
}
{
"interfacename":"1",
"data": "{\"aa\" : \"张三\"}"
}
-------------------------------------PortalVo 实体类-------------------------------------
private String interfacet;
Private String data;
-------------------------------------工具类-------------------------------------
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ThirdPartyInterfaceUtils {
/**
* 调取外系统接口的数据
* @param date
* @return
*/
public static Map<String,Object> getExternalInfo(String infoUrl, String date){
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
//配置超时时间
RequestConfig requestConfig = RequestConfig.custom().
setConnectTimeout(300000).setConnectionRequestTimeout(300000)
.setSocketTimeout(300000).setRedirectsEnabled(true).build();
HttpPost httpPost = new HttpPost(infoUrl);
httpPost.headset("content-type", "application/x-www-form-urlencoded;catharses=UTF-8");
//设置超时时间
httpPost.setConfig(requestConfig);
//装配post请求参数
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("date",date));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
//设置post求情参数
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String resData = EntityUtils.toString(httpEntity);
System.out.println("返回信息 =======" + resData);
JSONObject o = JSONObject.parseObject(resData);
Map<String,Object> map = new HashMap<>();
map = o;
return map;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
-------------------------------------Maven-------------------------------------
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<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.47</version>
</dependency>
-------------------------------------B 接收端,接收a请求-------------------------------------
@RequestMapping("/initDataByNums")
@ResponseBody
public Map<String,String> initDataByNums(HttpServletRequest request) {
String pa = "";
JSONObject jsonObject = null;
try {
InputStream in = request.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] b = new byte[1024 * 8];
int len;
while ((len = in.read(b)) != -1){
byteArrayOutputStream.write(b, 0, len);
}
pa = new String(byteArrayOutputStream.toByteArray());//拿到值
pa = URLDecoder.decode(pa, "utf-8");//去掉符号,中文乱码
pa = pa.substring(5,pa.length());//去掉data={}
pa = pa.replaceAll("=",":");//等号换冒号
jsonObject = JSONObject.fromObject(pa);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(jsonObject.get("aa"));//接收到了
Map map1 = new HashMap();//假设一个返回值
map1.put("aaa","unifiedEntrance");
return map1;
}