JAVA携带参数(带有请求参数,请求头参数)直接发送POST请求
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>
package com.test; import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import java.util.ArrayList;
import java.util.List; public class Test {
public static void main(String[] args) {
sendPost();
} public static void sendPost(){
String value="12222"; //创建post请求对象
HttpPost post = new HttpPost("http://localhost:8080/test");
try {
//创建参数集合
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
//添加参数
list.add(new BasicNameValuePair("key", value));
list.add(new BasicNameValuePair("releaseDate","2020-07-14 09:55:20"));
//把参数放入请求对象,,post发送的参数list,指定格式
post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
//添加请求头参数
post.addHeader("timestamp","1594695607545");
CloseableHttpClient client = HttpClients.createDefault();
//启动执行请求,并获得返回值
CloseableHttpResponse response = client.execute(post);
//得到返回的entity对象
HttpEntity entity = response.getEntity();
//把实体对象转换为string
String result = EntityUtils.toString(entity, "UTF-8");
//返回内容
System.out.println(result);
} catch (Exception e1) {
e1.printStackTrace(); }
}
}
下面是把方法进行了封装
/**
*
* 发送请求
* @param url 发送的url
* @param headerMap 请求头参数集合 key参数名 value为参数值
* @param bodyMap 请求参数集合 key参数名 value为参数值
*/
public static String sendPost(String url, Map<String,String> headerMap,Map<String,String> bodyMap){ //创建post请求对象
HttpPost post = new HttpPost(url);
try {
//创建参数集合
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
//添加参数
if (bodyMap!=null){
for (String str:bodyMap.keySet()
) {
list.add(new BasicNameValuePair(str, bodyMap.get(str)));
}
}
//把参数放入请求对象,,post发送的参数list,指定格式
post.setEntity(new UrlEncodedFormEntity(list, "UTF-8")); if (headerMap!=null){
for (String str:headerMap.keySet()
) {
post.addHeader(str,headerMap.get(str));
}
} CloseableHttpClient client = HttpClients.createDefault();
//启动执行请求,并获得返回值
CloseableHttpResponse response = client.execute(post);
//得到返回的entity对象
HttpEntity entity = response.getEntity();
//把实体对象转换为string
String result = EntityUtils.toString(entity, "UTF-8");
//返回内容
return result;
} catch (Exception e1) {
e1.printStackTrace();
return ""; }
}
JAVA携带参数(带有请求参数,请求头参数)直接发送POST请求的更多相关文章
- ReadyAPI/soapUI发送post请求json格式(带有中文字符),后台获取参数为空
解决:请求编码格式默认为空,在"TestCase"的指定Step的Request Properties中, 改Encoding编码格式为UTF-8. 原文:soapUI发送post ...
- 后台发送http请求通用方法,包括get和post
package com.examsafety.service.sh; import java.io.BufferedReader; import java.io.IOException; import ...
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)
[正文] 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层 ...
- UrlConnection发送http请求 中文乱码解决
中文乱码 DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream()); //dos.writeBytes(jsonD ...
- 【python接口自动化】- 使用requests库发送http请求
前言:什么是Requests ?Requests 是⽤Python语⾔编写,基于urllib,采⽤Apache2 Licensed开源协议的 HTTP 库.它⽐ urllib 更加⽅便,可以节约我们⼤ ...
- 使用Ajax发送http请求(get&post请求)
本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 同步和异步 同步和异步的概念 同步:必须等待前面的任务完成,才能继续后面 ...
- 每天一个linux命令13之curl发送http请求
一.get请求 curl "http://www.baidu.com" 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http:// ...
- 发送HTTP请求 -- HttpUtil
1. package com.step.utils; import java.io.IOException; import java.net.URLDecoder; import java.util. ...
随机推荐
- mingling
mysql> USE mon Reading table information for completion of table and column names You can turn of ...
- 一个画组织解剖图R包
地址: https://github.com/jespermaag/gganatogram
- C语言 fastq文件转换为fasta文件
目前只能处理短序列,若要处理长序列,可按照https://www.cnblogs.com/mmtinfo/p/13036039.html的读取方法. 1 #include <stdio.h> ...
- 基本绘图函数:plot的使用
注意:"##"后面是程序输出结果 例如: par("bg") # 命令 ## [1] "white" # 结果 基本绘图函数: plot:散 ...
- 使用SpringBoot实现文件的下载
上一篇博客:使用SpringBoot实现文件的上传 已经实现了文件的上传,所以紧接着就是下载 首先还是html页面的简单设计 <form class="form-signin" ...
- Spring整合Mybatis报 java.lang.ClassNotFoundException:org.springframework.core.metrics.ApplicationStartup,即:spring的版本过高,采用RELEASE稳定版
1.遇到的问题: 今天在弄spring整合mybatis的时候遇到一个小问题,如图所示: 简单来说:就是我的spring的xml文件没找到,我就奇了怪了,我所有的配置都没问题啊! 我pom.xml配置 ...
- college-ruled notebook
TBBT.s3.e10: Sheldon: Where's your notebook?Penny: Um, I don't have one.Sheldon: How are you going t ...
- HTTP请求 Java API
1.导入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId>common ...
- Angular @Input讲解及用法
1.什么是@input @input的作用是定义模块输入,是用来让父级组件向子组件传递内容. 2.@input用法 首先在子组件中将需要传递给父组件的变量用@input()修饰 需要在子组件ts文件i ...
- 视频框架 Vitamio使用
转自http://blog.csdn.net/u010181592/article/category/5893483 1.在https://github.com/yixia/VitamioBundle ...