java 模拟发送post请求测试
方法一: HttpClient
public void postTest(HttpServletRequest request,Integer type,String phone,String passwd,String schoolld,String agent){
String url = "xxxxxxxxx";//发送请求的URL地址
JSONObject jsonObject = new JSONObject();//封装参数
jsonObject.put("phone", "xxxx");
jsonObject.put("agent", "xxxx");
String params=jsonObject.toString();
sendPostHttpClient(url, params);
}
public String sendPostHttpClient(String url, String data) {
String response = null;
try {
CloseableHttpClient httpclient = null;
CloseableHttpResponse httpresponse = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
//StringEntity stringentity = new StringEntity(data, ContentType.create("text/json", "UTF-8"));参数传递文本格式
StringEntity stringentity = new StringEntity(data, ContentType.create("application/json", "UTF-8"));//参数传递json格式
httppost.setEntity(stringentity);
httpresponse = httpclient.execute(httppost);
response = EntityUtils.toString(httpresponse.getEntity());
logger.info("response: " + response);
} finally {
if (httpclient != null) {
httpclient.close();
}
if (httpresponse != null) {
httpresponse.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
方法二:RestTemplate
public Map<String,String> getShangHaiCarDealCount(String formatTime) {
Map<String,String> map=new HashMap<>();
map.put("flag", "fail");
String cycle = "1";
String pic_type = "3";
String qsrq = formatTime;
String Submit = "显示";
String zzrq = formatTime;
String result = null;
try {
String url = "http://xxxxxxxxxxxxxxxxxxxxxx";
RestTemplate client = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
// 请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 封装参数,千万不要替换为Map与HashMap,否则参数无法传递
MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
// 也支持中文
params.add("cycle", cycle);
params.add("pic_type", pic_type);
params.add("qsrq", qsrq);
params.add("Submit", Submit);
params.add("zzrq", zzrq);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
// 执行HTTP请求
ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
result = response.getBody();
map.put("result", result);
map.put("flag", "success");
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
java 模拟发送post请求测试的更多相关文章
- jmeter测试TCP服务器/模拟发送TCP请求
jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响应. jmeter模拟发送TCP请求的方法: 1. ...
- jmeter ---测试TCP服务器/模拟发送TCP请求
jmeter测试TCP服务器/模拟发送TCP请求 jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响 ...
- jmeter测试TCP服务器/模拟发送TCP请求 设置16进制发送(转)
转载留存:http://blog.sina.com.cn/s/blog_46d0362d0102v8ii.html 性能测试需要模拟多种场景,经常受制于资源限制,没办法建立贴近实际部署环境的场景.因而 ...
- 模拟发送http请求的工具推荐
做网站开发时,经常需要发送请求来测试自己的代码是否OK,这时候模拟发送http请求的工具就起到了很大的作用.特别是需要在请求带header时就更加的有必要使用工具.下面推荐的工具有的是基于系统开发的程 ...
- java httpclient发送json 请求 ,go服务端接收
/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...
- PHP模拟发送POST请求之五curl基本使用和多线程优化
今天来介绍PHP模拟发送POST请求的重型武器——cURL函数库的使用和其多线程的优化方法. 说起cURL函数,可谓是老生常谈,但网上许多资料都在关键部分语焉不详,列出一大堆手册上的东西,搞得我入门时 ...
- PHP模拟发送POST请求之一、HTTP协议头部解析
WEB开发中信息基本全是在POST与GET请求与响应中进行,GET因其基于URL的直观,易被我们了解,可POST请求因其信息的隐蔽,在安全的同时,也给开发者们模拟发送带来了麻烦.接下来的几篇博文中,我 ...
- WebClient模拟发送Post请求
WebClient模拟发送Post请求方法: /// <summary> /// 模拟post请求 /// </summary> /// <param name=&quo ...
- JAVA模拟HTTP post请求上传文件
在开发中,我们使用的比较多的HTTP请求方式基本上就是GET.POST.其中GET用于从服务器获取数据,POST主要用于向服务器提交一些表单数据,例如文件上传等.而我们在使用HTTP请求时中遇到的比较 ...
随机推荐
- Shell 利用 curl 模拟登陆
-b 参数 指定使用cookie文件 -c是往cookie文件中写cookie -d 是指定此次登录所需的参数,通过httpfox查看 -L 指定页面自动跳转 #curl -c ck.txt --us ...
- MVC 6 电商网站开发实战
[原创] ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API 标签: Web API MVC6 创建web API | 博主:powertoolsteam ASP.NE ...
- 039 DataFrame的理解
1.构成 由RDD+Schema构成 RDD: DataFrame中的数据 ===> df.rdd Schema: RDD中数据的结构 ===> df.schema df是dataFram ...
- MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介
网站或服务的性能关键点很大程度在于数据库的设计(假设你选择了合适的语言开发框架)以及如何查询数据上. 我们知道MySQL的性能优化方法,一般有建立索引.规避复杂联合查询.设置冗余字段.建立中间表.查询 ...
- ZOJ Monthly, March 2018 题解
[题目链接] A. ZOJ 4004 - Easy Number Game 首先肯定是选择值最小的 $2*m$ 进行操作,这些数在操作的时候每次取一个最大的和最小的相乘是最优的. #include & ...
- 七夕情人节表白-纯JS实现3D心形+图片旋转
七夕情人节就快到了,这里献上纯js表白神器-心里都是你,预览: 技术点:css-3d.js-随机色.js-transform 1.html: <div class="heart&quo ...
- 【Ray Tracing in One Weekend 超详解】 光线追踪1-3
学完了插值,我们来学习在场景里面添加一个立体彩色球(三维插值) 按照惯例,先看效果: Chapter4: Adding a sphere 我们又一次面临图形学的主要任务. 我们需要再次回顾coord1 ...
- 基于CommonsCollections4的Gadget分析
基于CommonsCollections4的Gadget分析 Author:Welkin 0x1 背景及概要 随着Java应用的推广和普及,Java安全问题越来越被人们重视,纵观近些年来的Java安全 ...
- 7、Redis中对ZSet类型的操作命令
写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- ---------- ...
- linux_远程copy
1:远程copy [linux对linux 远程拷贝] scp 文件名 root@远程ip:/路径/ 将本地home目录下的test.tar的文件拷贝到远程主机192.168.1.23的/ho ...