广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >Java请求调用参数格式为form-data类型的接口
  • 697
分享到

Java请求调用参数格式为form-data类型的接口

java开发语言 2023-08-20 09:08:23 697人浏览 八月长安
摘要

接口参数使用postman调用如图所示,只能使用form-data格式调用 使用java代码发送http请求实现此种方式的接口调用 public static String doPostFORM(String url, HashMap m

接口参数使用postman调用如图所示,只能使用form-data格式调用
postman实例
使用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属性为编码方式

  • 常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。
  • x-www-form-urlencoded:当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url。
  • multipart/form-data:当action为post时候,浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。

来源地址: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文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作