JAVA 模拟post 表单提交
一开始模拟http请求用的都是这个工具类
package com.bju.cms.common; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; 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.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; public class HttpUtils {/** * 最大连接数 */ private static final int MAX_CONN_TOTAL = 200; /** * 单个路由的最大连接数,默认是2 */ private static final int MAX_CONN_PERROUTE = 20; /** * 默认超时时间,毫秒 */ private static final int DEFAULT_TIMEOUT = 30000; /** * 默认编码 */ private static final String DEFAULT_CHARSET = "UTF-8"; /** * @param url * @param paramters * @param values * @param timeout * @return * @throws Exception */ public static String post(String url, String[] paramters, String[] values, int timeout) throws Exception {CloseableHttpResponse response = null; CloseableHttpClient client = null; String result = ""; try {PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setDefaultMaxPerRoute(MAX_CONN_PERROUTE); cm.setMaxTotal(MAX_CONN_TOTAL); client = HttpClients.custom().setConnectionManager(cm).build(); List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); for (int i = 0; i < paramters.length; i++) {params.add(new BasicNameValuePair(paramters[i], values[i])); }HttpPost httppost = new HttpPost(url); httppost.setEntity(new UrlEncodedFormEntity(params, Charset.forName(DEFAULT_CHARSET))); // 设置请求超时,连接超时和数据传输超时 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build(); httppost.setConfig(requestConfig); response = client.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) {HttpEntity responseEntity = response.getEntity(); result = EntityUtils.toString(responseEntity, DEFAULT_CHARSET); } else {throw new RuntimeException("Response status:" + statusCode); }} finally {if (response != null)response.close(); if (client != null)client.close(); }return result; }/** * 参数为json的post请求 * @param url * @param jsonData * @return * @throws Exception */ public static String post(String url, String jsonData, int timeout) throws Exception {CloseableHttpResponse response = null; CloseableHttpClient client = null; String result = ""; try {PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setDefaultMaxPerRoute(MAX_CONN_PERROUTE); cm.setMaxTotal(MAX_CONN_TOTAL); client = HttpClients.custom().setConnectionManager(cm).build(); HttpPost httppost = new HttpPost(url); HttpEntity requestEntity = new StringEntity(jsonData, Charset.forName(DEFAULT_CHARSET)); httppost.setEntity(requestEntity); // 设置超时时间 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build(); httppost.setConfig(requestConfig); response = client.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) {HttpEntity responseEntity = response.getEntity(); result = EntityUtils.toString(responseEntity, DEFAULT_CHARSET); } else {throw new RuntimeException("Response status:" + statusCode); }} finally {if (response != null)response.close(); if (client != null)client.close(); }return result; }/** * 通过header认证授权接口 * @param url * @param authorization * @param timeout * @return * @throws Exception */ public static String postByAuth(String url, String authorization, int timeout) throws Exception {CloseableHttpResponse response = null; CloseableHttpClient client = null; String result = ""; try {PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setDefaultMaxPerRoute(MAX_CONN_PERROUTE); cm.setMaxTotal(MAX_CONN_TOTAL); client = HttpClients.custom().setConnectionManager(cm).build(); HttpPost httppost = new HttpPost(url); httppost.setHeader("Authorization", authorization); // 设置超时时间 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build(); httppost.setConfig(requestConfig); response = client.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) {HttpEntity responseEntity = response.getEntity(); result = EntityUtils.toString(responseEntity, DEFAULT_CHARSET); } else {throw new RuntimeException("Response status:" + statusCode); }} finally {if (response != null)response.close(); if (client != null)client.close(); }return result; }/** * 支持contentType的请求 * @param url * @param data * @return * @throws Exception */ public static String post(String url, String data, String contentType) throws Exception {CloseableHttpResponse response = null; CloseableHttpClient httpclient = null; String result = ""; try {PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setDefaultMaxPerRoute(MAX_CONN_PERROUTE); cm.setMaxTotal(MAX_CONN_TOTAL); httpclient = HttpClients.custom().setConnectionManager(cm).build(); HttpPost httppost = new HttpPost(url); // 设置参数 StringEntity reqEntity = new StringEntity(data); reqEntity.setContentType(contentType); // 设置超时时间 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(DEFAULT_TIMEOUT).setConnectTimeout(DEFAULT_TIMEOUT).setSocketTimeout(DEFAULT_TIMEOUT).build(); httppost.setConfig(requestConfig); httppost.setEntity(reqEntity); response = httpclient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) {HttpEntity responseEntity = response.getEntity(); result = EntityUtils.toString(responseEntity, DEFAULT_CHARSET); } else {throw new RuntimeException("Response status:" + statusCode); }} finally {if (response != null)response.close(); if (httpclient != null)httpclient.close(); }return result; }/** * 支持https请求 * @param url * @param data * @param contentType * @return * @throws Exception */ @SuppressWarnings("deprecation")public static String postByHttps(String url, String data, String contentType) throws Exception {HttpClient client = null; PostMethod post = null; try {client = new HttpClient(); post = new PostMethod(url); post.setRequestHeader("Authorizatimon", "no-check-certificate"); post.setRequestHeader("Content-type", contentType); StringRequestEntity requestEntity = new StringRequestEntity(data); post.setRequestEntity(requestEntity); client.setTimeout(DEFAULT_TIMEOUT); client.setConnectionTimeout(DEFAULT_TIMEOUT); int status = client.executeMethod(post); if (status == HttpStatus.SC_OK) {return new String(post.getResponseBody(), DEFAULT_CHARSET); } else {throw new RuntimeException("Response status:" + status); }} finally {if (post != null)post.abort(); }}}
昨天模拟写一个post的表单提交 请求200 但是参数传不过去 后来在网上搜了很多 发现了另一种办法......
package com.bju.csair.common; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.List; public class HttpU {public static String postWithParamsForString(String url, List<NameValuePair> params){HttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); String s = ""; try {httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); HttpResponse response = client.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode==200){HttpEntity entity = response.getEntity(); s = EntityUtils.toString(entity); }} catch (IOException e) {e.printStackTrace(); }return s; } }
看到这个方法的时候突然想到原来好像是真的这么用过 用这个NameValuePair组装参数的
List<NameValuePair> params = new ArrayList<>(); NameValuePair jobGroup = new BasicNameValuePair("jobGroup", String.valueOf(pageListRequest.getJobGroup())); NameValuePair start = new BasicNameValuePair("start", pageListRequest.getStart()); NameValuePair length = new BasicNameValuePair("length", pageListRequest.getLength()); NameValuePair jobDesc = new BasicNameValuePair("length", pageListRequest.getJobDesc()); NameValuePair executorHandler = new BasicNameValuePair("length", pageListRequest.getExecutorHandler()); params.add(jobGroup); params.add(start); params.add(length); params.add(jobDesc); params.add(executorHandler);
JAVA 模拟post 表单提交相关推荐
- ajax 模拟表单提交,Ajax模拟Form表单提交,含多种数据上传
---恢复内容开始--- Ajax提交表单.使用FormData提交表单数据和上传的文件(这里的后台使用C#获取,你可以使用Java一样获取) 有时候前台的数据提交到后台,不想使用form表单上传,希 ...
- JS模拟Form表单提交
用java写了一个下载的功能,测试没有问题,但前台就是不弹出下载的提示框. 后来发现如果你的提交方式是ajax的方式的话是不会弹出提示框的,然后换成了form提交,顺利弹框通过,下来我们就用js模拟f ...
- 使用webmagic模拟post表单提交爬取易查分成绩
使用webmagic模拟post表单提交爬取易查分成绩 #不废话直接上图 简单来说就是在一图输入学生姓名 点击查询就会跳转到二图,当然这里二图我把班级姓名信息以及去掉了: 下面就是分析通过上图我们可以 ...
- js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题...
js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题 js模拟form表单提交数据源码: /** * js模拟form表单提交 * @param ...
- AJAX学习笔记——发送AJAX的POST请求,模拟from表单提交
关于AJAX发送POST请求,首先演示一个小案例. 当输入用户名:张三,密码:123.点击发送请求按钮 这是用post请求模拟的表单提交.接下来看一下如何用AJAX发送POST请求 后端代码: @We ...
- JS动态模拟Form表单提交数据
分享知识 传递快乐 JS动态模拟Form表单提交数据 <!DOCTYPE html> <html lang="en"> <head><m ...
- curl模拟form表单提交
curl模拟form表单提交 一. 首先,最简单的情况是我们只需要提交一个不带文件上传的表单,这种情况下,只需要在curl中使用–data(注意是–不是-)或者它的缩写-d即可. curl -d &q ...
- 跨域请求之JSP中模拟post表单提交
一.使用场景 当我们需要跨域进行登录时,为了避免登录信息暴露在链接中,此时必须采用Post提交.同时Ajax是不支持跨域的.此时就可以采用在Jsp中模拟Post提交. 二.代码实现 以下实例是跨域登录 ...
- Excel 模拟form表单提交
前端模拟form表单 function batSubmit() {var temp_form = document.createElement("form");//temp_for ...
最新文章
- 关于《强化狼群等级制度的灰狼优化算法》的问题邮件回复
- 构造函数不可以是虚函数;析构函数可以是虚函数,也可以是纯虚函数。
- windows live writer 插件 VSPaste 中文乱码和去空白链接方案
- 教你禁用右键,也教你如何破解
- Spring——原理解析-利用反射和注解模拟IoC的自动装配
- 如何通过玩TensorFlow Playground来理解神经网络
- 海康sdk java示例_调用海康SDK
- 如何按页进行PDF文档拆分
- “Master”围棋对战50胜1和,人工智能身份欲揭?
- 计算机辅助翻译小结,计算机辅助翻译
- 边缘服务网格 osm-edge
- 手把手教你设计短信验证码
- 关于计算机素养论文,计算机应用及青少年网络素养培养论文
- C++中使用代码修改字体颜色
- java中rate,Java务实际利率之Excel函数RATE
- 如何用自媒体引流?学会这3招足矣,日引500+精准粉
- 量化lstm为onnx遇到end值越界的解决方法
- 基于matlab数字处理系统设计新颖,基于MATLAB的单相光伏并网逆变系统的设计
- 欢迎来到我的游戏大厅
- 学会了海外抖音才发现:靠副业生活,真的不难 !