【已弃用】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

相关文章

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page