java发起application/x-www-form-urlencoded格式的post请求

  话不多说,直接上代码。

    public static String postXwwFormUrlEncoded(String url, HashMap<String, String> paraMap) throws IOException {
        List<NameValuePair> params = new ArrayList<>();
        HttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        String msg = "";
        try {
            if (paraMap != null && paraMap.size() != 0) {
                for (Map.Entry<String, String> entry : paraMap.entrySet()) {
                    params.add(new NameValuePair() {
                        @Override
                        public String getName() {
                            return entry.getKey();
                        }
                        @Override
                        public String getValue() {
                            return entry.getValue();
                        }
                    });
                }
            }
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));
            httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
            HttpResponse response = client.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (HttpURLConnection.HTTP_OK == statusCode) {
                HttpEntity entity = response.getEntity();
                msg = EntityUtils.toString(entity);
            }else {
            	return "{\"status\": \"0\",\"desc\": \"接口请求异常\"}";
            }
        } catch (IOException e) {
            throw e;
        }
        return msg;
    }

  其中url为接口请求地址,paraMap为参数Map。注意上面写法需要引入依赖

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
		</dependency>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java中发送application/x-www-form-urlencoded格式请求,可以使用HttpURLConnection或者HttpClient来实现。下面是使用HttpURLConnection的示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class Main { public static void main(String[] args) { try { // 创建URL对象 URL url = new URL("请求URL"); // 创建HttpURLConnection对象 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置请求方法为POST conn.setRequestMethod("POST"); // 设置请求头部 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 启用输入输出流 conn.setDoInput(true); conn.setDoOutput(true); // 构建请求参数 String params = "param1=" + URLEncoder.encode("value1", "UTF-8") + "&param2=" + URLEncoder.encode("value2", "UTF-8"); // 发送请求 OutputStream os = conn.getOutputStream(); os.write(params.getBytes("UTF-8")); os.flush(); // 获取响应结果 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; StringBuilder result = new StringBuilder(); while ((line = reader.readLine()) != null) { result.append(line); } // 关闭连接和流 reader.close(); os.close(); conn.disconnect(); // 输出响应结果 System.out.println(result.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` 请将代码中的"请求URL"替换为实际的请求URL,并根据实际情况修改请求参数和响应处理逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值