今天要给公司的api网关项目写一个简单的接口调用demo,用的是HttpUtils doget方法,贴一下简化版的代码
public class APITest { public static void main(String[] args) { //设置参数 Map<String, String> parameters=new HashMap<>(); parameters.put("APPLICANT","NOKIA CORP"); //如果api有些鉴权的话需要在header中放一些参数 String appKey=""; //设置路径 String host=""; //网关参数 Map<String,String> headers=new HashMap<String,String>(); headers.put("APP_KEY",appKey); HttpResponse response= null; try { //发送请求 response = HttpUtils.doGet(host,"",headers,parameters); //获取结果 HttpEntity httpEntity=response.getEntity(); String responseStr= EntityUtils.toString(httpEntity,"utf-8"); System.out.println(responseStr); } catch (Exception e) { e.printStackTrace(); } } }