package com.jeesite.modules.main.controller;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class Test {
public static void main( String[] args ) throws IOException {
Post2();
}
public static void Post2() throws IOException {
String param ="{\n" +
" \"UNITCOD\":\"101\",\n" +
" \"UPDATETIM\":\"2021-03-26\",\n" +
" \"PLANDTE\":\"2021-03-26\",\n" +
" \"RECORDTIM\":\"2021-03-26\",\n" +
" \"UPDATENAM\":\"赵泽凯\",\n" +
" \"MACHNUM3\":0,\n" +
" \"MACHNUM2\":20,\n" +
" \"MACHNUM1\":20,\n" +
" \"ID\":\"BDC5B85EB9080186E053AC101054639C\",\n" +
" \"MACHKNDCOD\":\"Q\",\n" +
" \"OPERNAM\":\"赵泽凯\"\n" +
"}";
// 1.拿到一个httpclient的对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2.设置请求方式和请求信息
HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/wcar/a/allData/saveByPort");
//2.1 提交header头信息
httpPost.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36");
//2.2设置类型:
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
//2.1 提交请求体
StringEntity se = new StringEntity(param, Charset.forName("utf-8"));
se.setContentType("application/json");
httpPost.setEntity(se);
// 3.执行请求
CloseableHttpResponse response = httpClient.execute(httpPost);
// 4.获取返回值
String html = EntityUtils.toString(response.getEntity(),Charset.forName("utf-8"));
// 5.打印
System.out.println(html);
}
}
request测试
最新推荐文章于 2023-11-27 16:21:17 发布