场景:A服务方法中调用B服务(跨域),方式为HttpPost。postman直接调用,查询B服务返回的数据如下:
实现步骤:
1、准备好B服务的URL,用org.apache.http.client的HttpClient方法,并设置header。
String url = seagull.getSupplierUrl();
HttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type","application/json");//设置header
2、将参数塞入body中,传给对方,其中有个坑,参数转StringEntity时,一定要设置字符集UTF_8,否则会取默认的ISO80009
JSONObject json = new JSONObject();
json.put("supplierName", supplierName);
StringEntity stringEntity = new StringEntity(json.toJSONString(