参考文章
https://blog.csdn.net/illbehere/article/details/72851045
1. 引入依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
2. 服务接口
@RequestMapping(value = \"/index\")
@ResponseBody
public String index(String id){
if(id.equals(\"a\")){
return id;
}else
System.out.print(\"index请求\");
return \"ok\";
}
3. HttpClent使用post传参
@RequestMapping(value = \"/get\")
@ResponseBody
public String get(String id) throws Exception {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair(\"id\", id));
HttpClient httpClient = HttpClients.createDefault();
String url = \"http://127.0.0.1:8082/index\";
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse res = httpClient.execute(httpPost);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
return result;
}
return null;
}
4. localhost:8080/get->localhost:8081/index
继续阅读与本文标签相同的文章
-
五大典型场景中的API自动化测试实践
2026-05-19栏目: 教程
-
历时五天用 SwiftUI 做了一款 APP,阿里工程师如何做的? | 9月5号栖夜读
2026-05-19栏目: 教程
-
CAD如何批量导出PDF文件?别说PDF了!GIF我都能给你导出来
2026-05-19栏目: 教程
-
活动回顾丨阿里云业务中台最佳实践沙龙圆满落幕
2026-05-19栏目: 教程
-
在CentOS里查看ssh的登录记录
2026-05-19栏目: 教程
