使用httpclient 抓包 提交表单 网络请求
来源:互联网 发布:神奇网络热血霸业下载 编辑:程序博客网 时间:2024/06/06 09:19
接了个私活,批量注册某app账号,使用fiddler抓包后,多线程批量注册,httpclient的请求工具类源码可以通用任何需要网络请求的地方,分享出来
package cn.zhaozhiguang.hepai.util;import java.io.File;import java.io.IOException; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.net.ssl.SSLContext;import org.apache.http.*; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.*; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.LayeredConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.conn.ssl.SSLContexts;import org.apache.http.conn.ssl.TrustStrategy;import org.apache.http.entity.ContentType;import org.apache.http.entity.mime.MultipartEntityBuilder;import org.apache.http.entity.mime.content.FileBody;import org.apache.http.entity.mime.content.StringBody;import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @SuppressWarnings("deprecation")public class HttpUtil {private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class); private static int SocketTimeout = 30000;//30秒 private static int ConnectTimeout = 30000;//30秒 private static Boolean SetTimeOut = true; private static CloseableHttpClient getHttpClient() { RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create(); ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory(); registryBuilder.register("http", plainSF); //指定信任密钥存储对象和连接套接字工厂 try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); //信任任何链接 TrustStrategy anyTrustStrategy = new TrustStrategy() { public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }; SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, anyTrustStrategy).build(); LayeredConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); registryBuilder.register("https", sslSF); } catch (KeyStoreException e) { throw new RuntimeException(e); } catch (KeyManagementException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } Registry<ConnectionSocketFactory> registry = registryBuilder.build(); //设置连接管理器 PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry); //构建客户端 return HttpClientBuilder.create().setConnectionManager(connManager).build(); } /** * get * * @param url 请求的url * @param queries 请求的参数,在浏览器?后面的数据,没有可以传null * @return * @throws IOException */ public static String get(String url, Map<String, String> queries) throws IOException { String responseBody = ""; CloseableHttpClient httpClient = getHttpClient(); StringBuilder sb = new StringBuilder(url); if (queries != null && queries.keySet().size() > 0) { boolean firstFlag = true; Iterator<Map.Entry<String, String>> iterator = queries.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); if (firstFlag) { sb.append("?" + (String) entry.getKey() + "=" + (String) entry.getValue()); firstFlag = false; } else { sb.append("&" + (String) entry.getKey() + "=" + (String) entry.getValue()); } } } HttpGet httpGet = new HttpGet(sb.toString()); if (SetTimeOut) { RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(SocketTimeout) .setConnectTimeout(ConnectTimeout).build();//设置请求和传输超时时间 httpGet.setConfig(requestConfig); } try { logger.debug("Executing request " + httpGet.getRequestLine()); //请求数据 CloseableHttpResponse response = httpClient.execute(httpGet); int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); responseBody = EntityUtils.toString(entity); logger.debug(responseBody); EntityUtils.consume(entity); } else { logger.debug("http return status error:" + status); throw new ClientProtocolException("Unexpected response status: " + status); } } catch (Exception ex) { logger.debug("get请求时发生错误~"); } finally { httpClient.close(); } return responseBody; } /** post * @param url 请求的url * @param queries 请求的参数,在浏览器?后面的数据,没有可以传null * @param params post form 提交的参数 * @return * @throws IOException */ public static String post(String url, Map<String, String> queries, Map<String, String> params) throws IOException { String responseBody = ""; CloseableHttpClient httpClient = getHttpClient(); StringBuilder sb = new StringBuilder(url); if (queries != null && queries.keySet().size() > 0) { boolean firstFlag = true; Iterator<Map.Entry<String, String>> iterator = queries.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); if (firstFlag) { sb.append("?" + (String) entry.getKey() + "=" + (String) entry.getValue()); firstFlag = false; } else { sb.append("&" + (String) entry.getKey() + "=" + (String) entry.getValue()); } } } //指定url,和http方式 HttpPost httpPost = new HttpPost(sb.toString()); if (SetTimeOut) { RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(SocketTimeout) .setConnectTimeout(ConnectTimeout).build();//设置请求和传输超时时间 httpPost.setConfig(requestConfig); } //添加参数 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if (params != null && params.keySet().size() > 0) { Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next(); nvps.add(new BasicNameValuePair((String) entry.getKey(), (String) entry.getValue())); } } httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); //请求数据 CloseableHttpResponse response = httpClient.execute(httpPost); try { logger.debug("Executing request " + httpPost.getRequestLine()); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); responseBody = EntityUtils.toString(entity); logger.debug(responseBody); EntityUtils.consume(entity); } else { logger.debug("http return status error:" + response.getStatusLine().getStatusCode()); } } catch (Exception e) { logger.debug("post请求时发生错误~"); } finally { response.close(); } return responseBody; } /** * 表单提交 * @param url * @param queries * @param params * @param files * @return * @throws Exception */ public static String postForm(String url, Map<String, String> queries, Map<String, String> params, Map<String, File> files) throws Exception { String responseBody = ""; CloseableHttpClient httpClient = getHttpClient(); StringBuilder sb = new StringBuilder(url); if (queries != null && queries.keySet().size() > 0) { boolean firstFlag = true; Iterator<Map.Entry<String, String>> iterator = queries.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); if (firstFlag) { sb.append("?" + (String) entry.getKey() + "=" + (String) entry.getValue()); firstFlag = false; } else { sb.append("&" + (String) entry.getKey() + "=" + (String) entry.getValue()); } } } //指定url,和http方式 HttpPost httpPost = new HttpPost(sb.toString()); if (SetTimeOut) { RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(SocketTimeout) .setConnectTimeout(ConnectTimeout).build();//设置请求和传输超时时间 httpPost.setConfig(requestConfig); } MultipartEntityBuilder form = MultipartEntityBuilder.create(); if (params != null && params.keySet().size() > 0) { Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); StringBody body = new StringBody((String) entry.getValue(), ContentType.create("text/plain", Consts.UTF_8)); form.addPart((String) entry.getKey(), body); } } if(files != null && files.keySet().size() > 0){ Iterator<Map.Entry<String, File>> iterator = files.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<String, File> entry = iterator.next(); FileBody body9 = new FileBody(entry.getValue()); form.addPart(entry.getKey(), body9); } } HttpEntity entity = form.build(); httpPost.setEntity(entity); //请求数据 CloseableHttpResponse response = httpClient.execute(httpPost); try { logger.debug("Executing request " + httpPost.getRequestLine()); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity responseEntity = response.getEntity(); responseBody = EntityUtils.toString(responseEntity); logger.debug(responseBody); EntityUtils.consume(responseEntity); } else { logger.debug("http return status error:" + response.getStatusLine().getStatusCode()); } } catch (Exception e) { logger.debug("post表单时发生错误~"); } finally { response.close(); } return responseBody; }; }
阅读全文
0 0
- 使用httpclient 抓包 提交表单 网络请求
- 网络爬虫抓包使用及通过表单请求
- HttpClient与网络抓包wireshark
- 使用Charles进行网络请求抓包解析
- 使用Fiddler针对Android手机网络请求抓包
- wireshark 抓包发现 httpclient 提交的问题
- 使用HttpClient方式请求网络
- 使用HttpClient方式请求网络
- android中使用httpclient提交表单
- android中使用httpclient提交表单
- 使用httpclient模拟表单提交,上传图片
- android中使用httpclient提交表单
- Android 使用HttpClient提交表单数据
- 使用httpclient提交表单方式上传文件
- HttpClient使用get、post提交请求
- httpClient 模拟表单提交
- 使用Charles抓https请求包
- android使用Charles抓包https请求
- html_导航框架
- rowid去重(删除表的重复记录)
- opencv(31)---SIFT特征匹配
- 贪心法解题的经典习题
- 获取当前服务器的ip地址
- 使用httpclient 抓包 提交表单 网络请求
- 动态创建100个链表,5秒之后倒序展现
- 快速排序
- OpenStack Newton版本部署 ---- 镜像服务(glance)
- JS简易日历
- JAVA笔记-14
- OpenStack Newton版本部署----计算服务(nova)
- Algorithm头文件中的sort函数使用
- C++随记(一)---字符串数组的输入问题