远程接口调用

阅读: 评论:0

远程接口调用

远程接口调用

远程接口调用

在服务端去调用第三方远程的接口。

apache的HttpClient组件

  • 加httpClient的jar包

public class HttpClientUtil {public static String doGet(String path) throws Exception {String result = null;//http客户端,类似于postMan,浏览器CloseableHttpClient client = ateDefault();//请求对象HttpGet request = new HttpGet(path);//添加请求头,当客户端关闭时,通知服务端该连接可释放request.setHeader("Connection","close");//发送请求,返回响应对象HttpResponse response = ute(request);//响应的状态行StatusLine statusLine = StatusLine();HttpEntity respEntity = null;StatusCode()==HttpStatus.SC_OK){respEntity = Entity();//响应体//读取响应体对象中的数据result= String(respEntity,"UTF-8");}//客户端释放资源,断开连接,通知服务端连接已断开if(respEntity!=null){sume(respEntity);}if(response!=null){((CloseableHttpResponse) response).close();}if(request!=null){request.abort();}if(client!=null){client.close();}return result;}public static String doPost(String path,Map<String,String> map) throws Exception {String result = null;//http客户端,类似于postMan,浏览器CloseableHttpClient client = ateDefault();HttpPost request = new HttpPost(path);//请求对象//添加请求头,当客户端关闭时,通知服务端该连接可释放request.setHeader("Connection","close");//StringEntity,传递json字符串数据//UrlEncodedFormEntity,传递key=value数据HttpEntity requestEntity= new JSONString(map),"UTF-8");request.setEntity(requestEntity);request.setHeader("Content-type","application/json");//发送请求,返回响应对象HttpResponse response = ute(request);//响应的状态行StatusLine statusLine = StatusLine();HttpEntity respEntity = null;StatusCode()==HttpStatus.SC_OK){respEntity = Entity();//响应体//读取响应体对象中的数据result= String(respEntity,"UTF-8");}//客户端释放资源,断开连接,通知服务端连接已断开if(respEntity!=null){sume(respEntity);}if(response!=null){((CloseableHttpResponse) response).close();}if(requestEntity!=sume(requestEntity);if(request!=null){request.abort();}if(client!=null){client.close();}return result;}
}--------------------------------------------------------------------------------//doPut  方法名字改掉  请求对象改  其他跟post一样public static String doPut(String path,Map<String,String> map) throws Exception {}HttpPut request = new HttpPut(path);//请求对象//doDel  方法名字改掉  请求对象改  其他跟post一样public static String doDel(String path) throws Exception {}HttpDelete request = new HttpDelete(path);//请求对象
public class TestHttpClient {public static void main(String[] args) throws Exception {
//        String path = "=db3d22eebbff236d1b3b424ea12c3dd6&city=郑州";
//        String result = HttpClientUtil.doGet(path);
//        System.out.println(result);String path = "localhost:8080/user/login";Map<String,String> map = new HashMap<>();map.put("uphone","545555");map.put("valicode","22343");//执行path请求,同时把map中的数据转json放在请求体String s = HttpClientUtil.doPost(path, map);System.out.println(s);}
}

spring的RestTemplate工具类

//get接口
String path ="";
RestTemplate t = new RestTemplate();
String result= t.getForObject(path, String.class);
System.out.println(result);
后端接口的处理器方法:
@PostMapping("/addEmployee")
public Employee postRequest(@RequestBody Employee e){System.out.println("post请求");return e;
}//post接口
String uri = "localhost:8080/1011sm/addEmployee";
Employee newEmploy = new Employee("javasm", "28", "javasm@email");
RestTemplate restTemplate = new RestTemplate();
Employee result = restTemplate.postForObject(uri, newEmploy, Employee.class);
System.out.println(result);

本文发布于:2024-01-28 01:10:06,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063754123753.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:远程调用接口
标签:接口
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23