接口参数使用postman调用如图所示,只能使用form-data格式调用 使用java代码发送http请求实现此种方式的接口调用 public static String doPostFORM(String url, HashMap m
接口参数使用postman调用如图所示,只能使用form-data格式调用
使用java代码发送http请求实现此种方式的接口调用
public static String doPostFORM(String url, HashMap<String, String> map) throws Exception { String result = ""; CloseableHttpClient client = null; CloseableHttpResponse response = null; RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(550000).setConnectTimeout(550000) .setConnectionRequestTimeout(550000).setStaleConnectionCheckEnabled(true).build(); client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build(); // client = HttpClients.createDefault(); URIBuilder uriBuilder = new URIBuilder(url); HttpPost httpPost = new HttpPost(uriBuilder.build()); httpPost.setHeader("Connection", "Keep-Alive"); httpPost.setHeader("Charset", "UTF-8"); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); List<NameValuePair> params = new ArrayList<NameValuePair>(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); NameValuePair pair = new BasicNameValuePair(entry.geTKEy(), entry.getValue()); params.add(pair); } httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); try { response = client.execute(httpPost); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, "UTF-8"); } } } catch (ClientProtocolException e) { throw new RuntimeException("创建连接失败" + e); } catch (IOException e) { throw new RuntimeException("创建连接失败" + e); } return result; }
特别说明:form的Content-Type属性为编码方式
来源地址:https://blog.csdn.net/weixin_44949150/article/details/129408883
--结束END--
本文标题: Java请求调用参数格式为form-data类型的接口
本文链接: https://www.lsjlt.com/news/376335.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0