【已弃用】http post 方法传递参数的2种方式(api接口,或者说postman的请求)

本文主要是介绍【已弃用】http post 方法传递参数的2种方式(api接口,或者说postman的请求),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http post 方法传递参数的2种方式

  • StringEntity
  • UrlEncodedFormEntity
  • 项目实例

StringEntity

try{  HttpPost httpPost = new HttpPost(url);  //param参数,可以为param="key1=value1&key2=value2"的一串字符串,或者是jsonObject String param1="key1=value1&key2=value2"JSONObject param2= new JSONObject();  param2.put("key1", "value1");  param2.put("key2t"," value2");  StringEntity stringEntity = new StringEntity(param1);  StringEntity stringEntity = new StringEntity(param2.toString());  stringEntity.setContentType("application/x-www-form-urlencoded");  httpPost.setEntity(stringEntity);  HttpClient client = new DefaultHttpClient();   HttpResponse httpResponse = client.execute(httpPost);  String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);  } catch(IOException e){  }  

UrlEncodedFormEntity

// An highlighted blockList<NameValuePair> pairs = new ArrayList<NameValuePair>();  NameValuePair pair1 = new BasicNameValuePair("supervisor", supervisorEt.getEditableText().toString());  NameValuePair pair2 = new BasicNameValuePair("content", superviseContentEt.getEditableText().toString());  NameValuePair pair3 = new BasicNameValuePair("userId", String.valueOf(signedUser.getId()));  pairs.add(pair1);  pairs.add(pair2);  pairs.add(pair3);  httpPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8)) 

项目实例

需要导入的jar包
在这里插入图片描述

import com.dtstack.flinkx.exception.WriteRecordException;
import com.dtstack.flinkx.outputformat.BaseRichOutputFormat;
import com.dtstack.flinkx.restapi.common.HttpUtil;
import com.dtstack.flinkx.util.ExceptionUtil;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.flink.types.Row;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;import java.io.IOException;
import java.util.*;

//getRequest

HttpRequestBase request = HttpUtil.getRequest(method, requestBody, header, url);
/* 20210803 wx 修改post访问api josn参数无法传输的问题*/public static HttpRequestBase getRequest(String method,Map<String, Object> requestBody,Map<String, String> header,String url) {LOG.debug("current request url: {}  current method:{} \n", url, method);HttpRequestBase request = null;if (HttpMethod.GET.name().equalsIgnoreCase(method)) {request = new HttpGet(url);} else if (HttpMethod.POST.name().equalsIgnoreCase(method)) {HttpPost post = new HttpPost(url);post.setEntity(getEntityData(requestBody));request = post;} else {throw new UnsupportedOperationException("Unsupported method:" + method);}for (Map.Entry<String, String> entry : header.entrySet()) {request.addHeader(entry.getKey(), entry.getValue());}return request;}

//getEntityData

post.setEntity(getEntityData(requestBody));
public static StringEntity getEntityData(Map<String, Object> body) {String params =  jsonToParams(String.valueOf(gson.toJson(body.get("json"))));StringEntity stringEntity = new StringEntity(params, StandardCharsets.UTF_8);//stringEntity.setContentEncoding(StandardCharsets.UTF_8.name());这里设置了UTF-8反而不可以了return stringEntity;
}

//jsonToParams

String params =  jsonToParams(String.valueOf(gson.toJson(body.get("json"))));
/*** 20210803* json 转化为params 的格式 param参数,可以为"key1=value1&key2=value2"的一串字符串* @param jsonStr* @return*/public static String jsonToParams(String jsonStr){String params = null;try {Map<String,Object> map2= JSONObject.parseObject(jsonStr, HashMap.class);for (Map.Entry<String, Object> entry : map2.entrySet()) {System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());if(params==null){params = entry.getKey()+"="+String.valueOf(entry.getValue());}else{params = params +"&"+ entry.getKey()+"="+String.valueOf(entry.getValue());}}return params;}catch (Exception ex){throw new RuntimeException("json转map出错------------------------------------------------------------",ex);}}

这篇关于【已弃用】http post 方法传递参数的2种方式(api接口,或者说postman的请求)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1005201

相关文章

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st

Olingo分析和实践之ODataImpl详细分析(重要方法详解)

《Olingo分析和实践之ODataImpl详细分析(重要方法详解)》ODataImpl.java是ApacheOlingoOData框架的核心工厂类,负责创建序列化器、反序列化器和处理器等组件,... 目录概述主要职责类结构与继承关系核心功能分析1. 序列化器管理2. 反序列化器管理3. 处理器管理重要方

Python错误AttributeError: 'NoneType' object has no attribute问题的彻底解决方法

《Python错误AttributeError:NoneTypeobjecthasnoattribute问题的彻底解决方法》在Python项目开发和调试过程中,经常会碰到这样一个异常信息... 目录问题背景与概述错误解读:AttributeError: 'NoneType' object has no at

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

RabbitMQ消息总线方式刷新配置服务全过程

《RabbitMQ消息总线方式刷新配置服务全过程》SpringCloudBus通过消息总线与MQ实现微服务配置统一刷新,结合GitWebhooks自动触发更新,避免手动重启,提升效率与可靠性,适用于配... 目录前言介绍环境准备代码示例测试验证总结前言介绍在微服务架构中,为了更方便的向微服务实例广播消息,

Nginx安全防护的多种方法

《Nginx安全防护的多种方法》在生产环境中,需要隐藏Nginx的版本号,以避免泄漏Nginx的版本,使攻击者不能针对特定版本进行攻击,下面就来介绍一下Nginx安全防护的方法,感兴趣的可以了解一下... 目录核心安全配置1.编译安装 Nginx2.隐藏版本号3.限制危险请求方法4.请求限制(CC攻击防御)

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法