java send post from 发送模拟表单 工具类 自定义 headers 自定义 参数 采用二位数组传值

·  阅读 108

目录

介绍

代码

依赖包

基础发post请求 NameValuePair[]  参数

调用代码

模拟表单  spring[][] headers  NameValuePair[]   参数

调用代码

模拟表单 加 模拟headers String [][] 加参数 String[][] 

调用服务代码

 几个重要的headers

自定义headers 核心重点代码

自定义params 参数 重点代码


介绍

1.添加依赖

2.添加对应的代码到项目

3.使用对应的自定义headers or params 方法

4.post 模拟表单这个要加上 Content-Type", "application/x-www-form-urlencoded;charset=utf-8

5.加载headers中

6.或者使用如下这个地方 浏览器开发者模式的 request headers中拷贝使用

7.把其中的每一行放到数组中使用

8.很多爬取数据的时候有些网站就在这里做了设置

9.所以直接拷贝也方便,或每行搞过去

10.  ok

 

 

代码

依赖包


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
复制代码

 

基础发post请求 NameValuePair[]  参数

/**
	 * java 模拟表单 发送 post 
	 * 
	 * @author jianghy
	 * 
	 * 示例:
	        NameValuePair[] param = new NameValuePair[]{
		           		new NameValuePair("xmlData", "")
		    };
		    String url = "https://kns.cnki.net/kns8/Brief/GetGridTableHtml";
		    String context = sendPostRequest(url, param);
		    
	 * @param url 地址
	 * @param param 参数
	 * 
	 * @throws 
	 */
	public static String sendPostForm(String url, NameValuePair[] param) {
		String responseStr = "";
		try {
			HttpClient httpClient = new HttpClient();
			PostMethod postMethod = new PostMethod(url);
			postMethod.setRequestHeader("accept", "*/*");
			postMethod.setRequestHeader("connection", "Keep-Alive");
			postMethod.setRequestHeader("yonghong-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
			postMethod.setRequestHeader("Content-Type",
					"application/x-www-form-urlencoded;charset=utf-8");
			// 将表单的值放入postMethod中
			postMethod.setRequestBody(param);
			// 执行postMethod
			int statusCode = httpClient.executeMethod(postMethod);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					postMethod.getResponseBodyAsStream(), "utf-8"));
			StringBuffer sb = new StringBuffer();
			int len;
			while ((len = in.read()) != -1) {
				sb.append((char) len);
			}
			responseStr = sb.toString();
			in.close();
			postMethod.releaseConnection();
			
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return responseStr;
	}
复制代码

 

调用代码

 NameValuePair[] param = new NameValuePair[]{
		           		new NameValuePair("xmlData", "")
		    };
		    String url = "https://kns.cnki.net/kns8/Brief/GetGridTableHtml";
		    String context = sendPostForm(url, param);
复制代码

 

 

模拟表单  spring[][] headers  NameValuePair[]   参数

/**
	 * java 模拟表单 发送 post  自定义 headers
	 * 
	 * @author jianghy
	 * 
	 * 
	 * 示例:
	        NameValuePair[] param = new NameValuePair[]{
		           		new NameValuePair("xmlData", "")
		    };
		    String url = "https://kns.cnki.net/kns8/Brief/GetGridTableHtml";
		    String[][] headers = {{"key0","val0"},{"key1","val1"}};
		    String context = sendPostFormCustomHeaders(url, param);
		    
	 * @param url 地址
	 * @param param 参数
	 * @param headers  headers
	 *  
	 * @throws 
	 */
	public static String sendPostFormCustomHeaders(String url, NameValuePair[] param,String[][] headers)
			 {
		String responseStr = "";
		try {
			HttpClient httpClient = new HttpClient();
			PostMethod postMethod = new PostMethod(url);
			if(headers.length==0){
				postMethod.setRequestHeader("accept", "*/*");
				postMethod.setRequestHeader("connection", "Keep-Alive");
				postMethod.setRequestHeader("yonghong-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
				postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
				postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			}else{
				for (int i = 0; i < headers.length; i++) {
					postMethod.setRequestHeader(headers[i][0],headers[i][1]);
				}
			}
			
			// 将表单的值放入postMethod中
			postMethod.setRequestBody(param);
			// 执行postMethod
			int statusCode = httpClient.executeMethod(postMethod);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					postMethod.getResponseBodyAsStream(), "utf-8"));
			StringBuffer sb = new StringBuffer();
			int len;
			while ((len = in.read()) != -1) {
				sb.append((char) len);
			}
			responseStr = sb.toString();
			in.close();
			postMethod.releaseConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return responseStr;
	}
复制代码

 

调用代码

 NameValuePair[] param = new NameValuePair[]{
		           		new NameValuePair("xmlData", "")
		    };
		    String url = "https://kns.cnki.net/kns8/Brief/GetGridTableHtml";
		    String[][] headers = {{"key0","val0"},{"key1","val1"}};
		    String context = sendPostRequest(url, param);
复制代码

 

模拟表单 加 模拟headers String [][] 加参数 String[][] 

/**
	 * java 模拟表单 发送 post  自定义 headers
	 * 
	 * @author jianghy
	 * 
	 * 示例:
		    String url = "https://ksk.cks.com/sls/slsuey";
		    
		    String[][] headers = {{"connection","Keep-Alive"}
		    	,{"Content-Type","application/x-www-form-urlencoded;charset=utf-8"}};
		    
		    String[][] param = {{"key0","val0"},{"key1","val1"}};
		      
		    String context = sendPostRequest(url, param);
		    
	 * @param url 地址 
	 * @param param 参数 String[][]
	 * @param headers  headers
	 * 
	 * @throws 
	 */
	public static String sendPostFormCustomHeadersAndParams(String url,String[][] headers,String[][] param)
			 {
		String responseStr = "";
		try {
			HttpClient httpClient = new HttpClient();
			PostMethod postMethod = new PostMethod(url);
			if(headers.length==0){
				postMethod.setRequestHeader("accept", "*/*");
				postMethod.setRequestHeader("connection", "Keep-Alive");
				postMethod.setRequestHeader("yonghong-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
				postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
				postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			}else{
				for (int i = 0; i < headers.length; i++) {
					postMethod.setRequestHeader(headers[i][0],headers[i][1]);
				}
			}
			NameValuePair[] params = null;
			if(param.length==0){
				params = new NameValuePair[]{
		           		new NameValuePair("", "")
					}; 
			}else{
				params =  new NameValuePair[param.length];
				for (int i = 0; i < param.length; i++) {
					params[i] = new NameValuePair(param[i][0], param[i][1]);
				}
			}
			
			// 将表单的值放入postMethod中
			postMethod.setRequestBody(params);
			// 执行postMethod
			int statusCode = httpClient.executeMethod(postMethod);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					postMethod.getResponseBodyAsStream(), "utf-8"));
			StringBuffer sb = new StringBuffer();
			int len;
			while ((len = in.read()) != -1) {
				sb.append((char) len);
			}
			responseStr = sb.toString();
			in.close();
			postMethod.releaseConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return responseStr;
	}
复制代码

 

调用服务代码

String url = "https://ksk.cks.com/sls/slsuey";
		    
		    String[][] headers = {{"connection","Keep-Alive"}
		    	,{"Content-Type","application/x-www-form-urlencoded;charset=utf-8"}};
		    
		    String[][] param = {{"key0","val0"},{"key1","val1"}};
		      
		    String context = sendPostFormCustomHeadersAndParams(url, headers,param);
复制代码

 

 几个重要的headers

	postMethod.setRequestHeader("accept", "*/*");
			postMethod.setRequestHeader("connection", "Keep-Alive");
			postMethod.setRequestHeader("yonghong-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
			postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
复制代码

 

自定义headers 核心重点代码

if(headers.length==0){
				postMethod.setRequestHeader("accept", "*/*");
				postMethod.setRequestHeader("connection", "Keep-Alive");
				postMethod.setRequestHeader("yonghong-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
				postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
				postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
			}else{
				for (int i = 0; i < headers.length; i++) {
					postMethod.setRequestHeader(headers[i][0],headers[i][1]);
				}
			}
复制代码

 

自定义params 参数 重点代码

NameValuePair[] params = null;
			if(param.length==0){
				params = new NameValuePair[]{
		           		new NameValuePair("", "")
					}; 
			}else{
				params =  new NameValuePair[param.length];
				for (int i = 0; i < param.length; i++) {
					params[i] = new NameValuePair(param[i][0], param[i][1]);
				}
			}
复制代码

 

 

 

 

 

ok

 

 

 

 

 

持续更新

 

 

 

 

 

 

分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改