关于HttpPost调用接口以body形式传递参数的方案

通过HttpPost 以body传参方式调用接口

##解决中文乱码问题

url:http://www.baidu.com/getContent
//将需要的body参数添加到map中
xmlStr:`<?xml version="1.0" encoding="UTF-8"?><APPROVEDATAINFO>
<SBLSH>12345678</SBLSH>
<ZZZZZ>370000000000</ZZZZZ>
<EXPRESSTYPE>解决中文乱码</EXPRESSTYPE>
<TIME>20230509181354</TIME>
</APPROVEDATAINFO>`
Map<String, String> requestParams = new HashMap<String,String>();
			requestParams.put("accessToken", token);
			requestParams.put("xmlStr", xmlStr);
public String httpURLConnectionPOST (String url, Map<String, String> requestParams) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
	    List<NameValuePair> params = new ArrayList<NameValuePair>();
	    for (Map.Entry<String, String> entry : requestParams.entrySet()) {
            params.add(new BasicNameValuePair((String) entry.getKey(),
                    (String) entry.getValue()));
        }
	    HttpPost post = new HttpPost(url);
	    //添加header参数
	    post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	    post.setHeader("kkk", "123");
	    post.setHeader("xxx", "456");
	    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf-8");
	    post.setEntity(formEntity);
	    CloseableHttpResponse response = httpClient.execute(post);
	    HttpEntity resEntity = response.getEntity();
	    String res = EntityUtils.toString(resEntity, "utf-8");
		return res;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 JavaHttpURLConnection 类来实现 HTTP POST 请求。以下是一个简单的示例: ``` import java.net.HttpURLConnection; import java.net.URL; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.IOException; public class HttpPostExample { public static void main(String[] args) throws IOException { String url = "http://example.com/api"; String postData = "param1=value1&param2=value2"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法为 POST con.setRequestMethod("POST"); // 设置请求头部信息 con.setRequestProperty("User-Agent", "Java client"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // 启用输出流 con.setDoOutput(true); // 将 POST 数据写入请求体 OutputStream os = con.getOutputStream(); os.write(postData.getBytes()); os.flush(); os.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 body: " + response.toString()); } } ``` 在上面的示例中,我们首先创建一个 URL 对象,用于表示要访问的 API 地址。然后,我们使用 HttpURLConnection 类来打开连,并设置请求方法为 POST下来,我们设置请求头部信息,启用输出流,并将 POST 数据写入请求体。最后,我们读取响应内容,并将其打印出来。 注意,这里的 POST 数据是以字符串形式传递的,可以根据实际需要进行修改。另外,如果 API 返回的是 JSON 格式的数据,可以使用 JSON 库来解析响应结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值