https://cngolon.iteye.com/blog/1571188
https://blog.csdn.net/buyaopingbixiazai/article/details/80680009
SimpleHttpClient httclient = new SimpleHttpClient();
Map<String, String> headers = new HashMap<String, String>();
=============================================================================================================================
String pwdTempSerCode = Base64Utils.encode(serviceSecret); String pwdTempRandCode = Base64Utils.encode(phoneCode); headers.put("Accept","text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"); headers.put("Accept-Encoding", "gzip, deflate, br");
headers.put("Accept-Language", "zh-CN,zh;q=0.9"); headers.put("Connection", "Keep-Alive"); headers.put("Content-Type", "*");
headers.put("Cookie", buffer.toString()); headers.put("Host", "shop.10086.cn"); headers.put("Referer", "https://shop.10086.cn/i/?f=billdetailqry&welcome=" + System.currentTimeMillis()); headers.put("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36"); headers.put("x-requested-with", "XMLHttpRequest"); // 开始发包 String rest = httclient.get("https://shop.10086.cn/i/v1/fee/detailbilltempidentjsonp/" + userNumber + "?callback=jQuery031587062895804485_" + System.currentTimeMillis() + "&pwdTempSerCode=" + pwdTempSerCode + "&pwdTempRandCode=" + pwdTempRandCode + "&captchaVal=" + catpy + "&_=" + System.currentTimeMillis(), headers);
这里我们写了一个通用工具类,喜欢的可以自己研究
package com.fjdata.util; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CookieStore; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.BasicCookieStore; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author 朱洪郑 * 通用请求类 * * */ public class SimpleHttpClient { private static Logger logger = LoggerFactory.getLogger(SimpleHttpClient.class); private static CloseableHttpClient httpClient; private static CookieStore cookieStore; static { cookieStore = new BasicCookieStore(); // 将CookieStore设置到httpClient中 httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); } public static String getCookie(String name){ List<Cookie> cookies = cookieStore.getCookies(); System.out.println(cookies); for(Cookie cookie : cookies){ if(cookie.getName().equalsIgnoreCase(name)){ return cookie.getValue(); } } return null; } /** * GET 请求 * @param url * @return * @throws ClientProtocolException * @throws IOException */ public static String get(String url) throws ClientProtocolException, IOException { HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); return EntityUtils.toString(httpResponse.getEntity()); } public static String get(String url,Map<String,String> headers) throws ClientProtocolException, IOException { HttpGet httpGet = new HttpGet(url); for(Map.Entry<String, String> entry : headers.entrySet()){ httpGet.addHeader(entry.getKey(), entry.getValue()); } HttpResponse httpResponse = httpClient.execute(httpGet); return EntityUtils.toString(httpResponse.getEntity()); } /** * POST 请求 * @param url * @param params * @param headers * @return * @throws ParseException * @throws IOException */ public static String post(String url, Map<String, Object> params, Map<String, String> headers){ String str = ""; try { HttpPost httpPost = new HttpPost(url); UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(getParam(params), "UTF-8"); httpPost.setEntity(postEntity); if(headers != null){ addHeaders(httpPost, headers); } HttpResponse httpResponse = httpClient.execute(httpPost); str = EntityUtils.toString(httpResponse.getEntity(),"UtF-8"); } catch (Exception e) { logger.error("推送失败,url:"+url,e); } return str; } public static String post(String url, Map<String, String> headers){ String str = ""; try { HttpPost httpPost = new HttpPost(url); if(headers != null){ addHeaders(httpPost, headers); } HttpResponse httpResponse = httpClient.execute(httpPost); str = EntityUtils.toString(httpResponse.getEntity(),"UtF-8"); } catch (Exception e) { logger.error("推送失败,url:"+url,e); } return str; } /** * POST 请求 * @param url * @param * @param * @return * @throws ParseException * @throws IOException */ public static String post1(String url, String str, String cookie) throws ParseException, IOException{ HttpClient httpClient = new HttpClient(); PostMethod post = new PostMethod(url); post.setRequestHeader("Cookie", cookie); post.setRequestHeader("bfw-ctrl", "json"); post.setRequestHeader("Connection", "keep-alive"); post.setRequestHeader("Content-Type", "text/json"); post.setRequestHeader("Host", "ebsnew.boc.cn"); post.setRequestHeader("Origin", "https://ebsnew.boc.cn"); post.setRequestHeader("Referer", "https://ebsnew.boc.cn/boc15/welcome_ele.html?v=20171227064943706&locale=zh&login=card&segment=1"); post.setRequestHeader("X-Requested-With", "XMLHttpRequest"); RequestEntity entity = new StringRequestEntity(str, "text/html", "utf-8"); // List list=new ArrayList<>(); //list.add(new org.apache.commons.httpclient.NameValuePair("",""),new org.apache.commons.httpclient.NameValuePair("","")); post.addParameter("_locale", "zh_CN" ); post.setRequestEntity(entity); httpClient.executeMethod(post); String result = post.getResponseBodyAsString(); return result; } /** * 参数 * @param parameterMap * @return */ private static List<NameValuePair> getParam(Map<String, Object> parameterMap) { List<NameValuePair> param = new ArrayList<NameValuePair>(); for(Map.Entry<String, Object> entry : parameterMap.entrySet()){ param.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()))); } return param; } /** * 添加头 * @param httpRequest * @param headerMap */ private static void addHeaders(HttpEntityEnclosingRequestBase httpRequest, Map<String, String> headerMap){ for(Map.Entry<String, String> entry : headerMap.entrySet()){ httpRequest.addHeader(entry.getKey(), entry.getValue()); } } }