java中post请求url拼接参数

实现

@GetMapping("/get001")
    public AjaxResult get001() throws IOException {
        System.out.println("开始进入接口");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append( Post请求地址 )
                .append("?")
                .append("orgGridEvent.id="+"6")
                .append("&address="+"dizhi")
                .append("&contentText="+"contentText")
                .append("&orgGridEvent.departmentNo=" + "371423102");
        // 如果不填写此转换格式,地址中包含 空格内容 转换失败,会出现下面的异常
        // java.lang.IllegalArgumentException: Illegal character in query at index
		// 说是我输入的数据有问题
		/*
		?     %3F
		&      %26
		|      %124
		=     %3D
		#     %23
		/      %2F
		+     %2B
		%      %25
		空格   %20
		*/
        String url = stringBuffer.toString().replaceAll(" ", "%20");
        System.out.println("开始进入post了");
        JSONObject jsonObject = EonUtils.doPostForUrl(url);
        System.out.println(jsonObject);
        return null;
    }

Post工具类

public static JSONObject doPostForUrl(String url){
        JSONObject jsonObject = null;
        String result = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        //每个接口的Content-Type可能不同
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        try {
            HttpResponse response = httpClient.execute(httpPost);
             result = EntityUtils.toString(response.getEntity());
            jsonObject = JSONObject.fromObject(result);
            return jsonObject;
        }catch (Exception e){
            e.printStackTrace();
            jsonObject = JSONObject.fromObject(result);
            return jsonObject;
        }
    }
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java ,可以使用 HttpURLConnection 类来执行 POST 请求,并将参数作为请求体发送。 以下是一个示例代码: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class HttpPostExample { public static void main(String[] args) throws Exception { // 定义请求参数 String url = "http://example.com/api"; String name = "John Doe"; int age = 30; String email = "john.doe@example.com"; // 创建连接对象 URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法为 POST con.setRequestMethod("POST"); // 设置请求头信息 con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // 构造请求体 String urlParameters = "name=" + URLEncoder.encode(name, "UTF-8") + "&age=" + age + "&email=" + URLEncoder.encode(email, "UTF-8"); // 发送请求体 con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); // 获取响应结果 int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 打印响应结果 System.out.println("Response Code : " + responseCode); System.out.println("Response : " + response.toString()); } } ``` 在上述代码,我们将请求参数拼接成一个字符串,并设置到请求发送。在发送请求体之前,我们需要设置 `con.setDoOutput(true)` 来允许输出数据。最后,我们通过 `con.getInputStream()` 获取响应结果,并将其以字符串的形式返回。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值