java写⼀个post⽅法_个⼈学习系列-java代码实现post请求最近在开发的时候发现了⼀个需求,就是要使⽤post请求去请求其他项⽬的接⼝,并且还需要带参。所以就研究了⼀下。
java代码实现post请求
1. 搭建springboot环境
1.l的配置
org.apache.httpcomponents
httpclient
4.5.10
1.2 test POST⽅法实现
@RunWith(SpringRunner.class)
@SpringBootTest
public class TbFreshScoreInfoEdgeTest {
@Test
public void httpPostRequest(){
//这⾥根据⾃⼰的需要进⾏数据的填充
String url = "IP地址+⽅法";
HttpClient client = ateDefault();
//默认post请求
HttpPost post = new HttpPost(url);
//拼接多参数
JSONObject json = new JSONObject();
json.put("projectId", "1");
json.put("imageName", "2");
json.put("rowId", 3);
json.put("totalVoidArea", 4);
json.put("rowArea", 5);
json.put("voidRadio", 6.0);
json.put("minusScore", 7);
json.put("updateTime", new Date());
String message = "[" + json + "]";
try {
post.addHeader("Content-type", "application/json; charset=utf-8");
post.setHeader("Accept", "application/json");
post.setEntity(new StringEntity(message, StandardCharsets.UTF_8));
HttpResponse httpResponse = ute(post);
HttpEntity entity = Entity();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
1.3 test GET⽅法实现
public void httpGetRequest() {
String url = "IP地址+⽅法+参数";
try {
HttpClient client = ateDefault();
HttpGet httpGet = new HttpGet(url);
BasicResponseHandler responseHandler = new BasicResponseHandler(); println("参数:" + ute(httpGet, responseHandler));
} catch (Exception e) {
学习java的学习方法e.printStackTrace();
}
}
⽅法就这些,参考了⽹上的⼀些资料。

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