public static String getHtmlByPost2(String url, String codes) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(url); httppost.setHeader("Accept", "*/*"); httppost.setHeader("Accept-Encoding", "gzip, deflate"); httppost.setHeader("Accept-Language", "zh-CN,zh;q=0.9"); httppost.setHeader("Connection", "keep-alive"); httppost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); httppost.setHeader("Cookie", "__cfduid=d788de813737581b85309e78edcb4311d1491017109; aliyungf_tc=AQAAAFdFGCTBew4AVl3sKqQ8eBpkf1Km; acw_tc=AQAAAEbwFS2VhQEAVl3sKi4q2hv9UPUY; PHPSESSID=9ks560p3cfsl4ijssdbs293vm0; acw_sc=58df42bb4cb66ad520a52d2d33feebd47c9e51ba; SXHNET=ILOVESXH@2"); httppost.setHeader("Host", "3123213213.shop:8200"); httppost.setHeader("Origin", "http://123123213.shop"); httppost.setHeader("Referer", "http://123213123.shop/"); httppost.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.160 Mobile Safari/537.36"); // 创建参数队列 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); String result = null; formparams.add(new BasicNameValuePair("codes", codes)); UrlEncodedFormEntity uefEntity; uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); httppost.setEntity(uefEntity); // System.out.println("executing request " + httppost.getURI()); CloseableHttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity, "UTF-8"); } response.close(); httpclient.close(); return result; } public static void main(String[] args) throws Exception { System.out.println(getHtmlByPost2("http://231.12321321.cn:8200/api/visitorStatistics", "5V3HXwyalRsy2IUDptzGGfl")); } 输出的信息如下图:
网站的原始请求如下:
/** * * @param webSite * @param api * @return account = BC-Kh7whgUzNFv5p77QwQh9U 2个请使用\n换行 */ public static String getCount(String webSite, String api, String account) { try { // 发送预检请求(OPTIONS 请求) URL optionsUrl = new URL(webSite + api); HttpURLConnection optionsConn = (HttpURLConnection) optionsUrl.openConnection(); optionsConn.setRequestMethod("OPTIONS"); int optionsResponseCode = optionsConn.getResponseCode(); System.out.println("OPTIONS Response Code: " + optionsResponseCode); // 发送实际的 POST 请求 URL postUrl = new URL(webSite + api); HttpURLConnection postConn = (HttpURLConnection) postUrl.openConnection(); postConn.setRequestMethod("POST"); postConn.setRequestProperty("Content-Type", "application/json"); JSONObject object = new JSONObject(); object.put("account", account); String jsonInputString = object.toString(); //String jsonInputString = "{\"account\": \"BC-Kh7whgUzNFv5p77QwQh9U\"}"; postConn.setDoOutput(true); DataOutputStream postOut = new DataOutputStream(postConn.getOutputStream()); postOut.writeBytes(jsonInputString); postOut.flush(); postOut.close(); int postResponseCode = postConn.getResponseCode(); System.out.println("POST Response Code: " + postResponseCode); BufferedReader postIn = new BufferedReader(new InputStreamReader(postConn.getInputStream())); String postInputLine; StringBuffer postResponse = new StringBuffer(); while ((postInputLine = postIn.readLine()) != null) { postResponse.append(postInputLine); } postIn.close(); System.out.println("POST Response: " + postResponse.toString()); return postResponse.toString(); // JSONObject item = JSONObject.fromObject(postResponse.toString()); // JSONObject data = (JSONObject) item.get("data"); // System.out.println(data.getString("code")); // System.out.println(data.getString("front_domain")); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 发送get 请求一个url返回 返回的内容 * */ public static String get(String url) { String html = null; CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建httpClient实例 HttpGet httpGet = new HttpGet(url); // 创建httpget实例 CloseableHttpResponse response = null; try { response = httpClient.execute(httpGet); // 执行http get请求 } catch (ClientProtocolException e) { // http协议异常 e.printStackTrace(); } catch (IOException e) { // io异常 e.printStackTrace(); } HttpEntity entity = response.getEntity(); // 获取返回实体 try { // System.out.println("网页内容:"+EntityUtils.toString(entity, // "utf-8")); // 获取网页内容 html = EntityUtils.toString(entity, "utf-8"); } catch (ParseException e) { // 解析异常 e.printStackTrace(); } catch (IOException e) { // io异常 e.printStackTrace(); } try { response.close(); // response关闭 } catch (IOException e) { // io异常 e.printStackTrace(); } try { httpClient.close(); // httpClient关闭 } catch (IOException e) { e.printStackTrace(); } return html; }
/** * body 这是json格式的数据。{"content":"34524"} * object.put("content", msg); * String body = object.toString(); * 传来的就是body 字符串 */ public static String post(String url, String body,String token ) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); // 这个地方要写, PARAMETERS这个参数是如何设置的 //https://openapi.chanjet.com/accounting/openapi/cc/book/findByEnterpriseId?queryType=BINDING_TO_THIRD_PLATFORM // 一般来说 post 是没有parameters 这个是get的参数。 // 这个地方要写, PARAMETERS这个参数是如何设置的 HttpPost httppost = new HttpPost(url); httppost.setHeader("appKey", PropertiesUtil.appKey); httppost.setHeader("appSecret", PropertiesUtil.appSecret); httppost.setHeader("Content-Type", "application/json"); if(StringUtil.isNotEmpty(token)){ httppost.setHeader("openToken", token); } String result = ""; if(StringUtil.isNotEmpty(body)){ //这是body参数。{page:1,limit:2} StringEntity entity = new StringEntity(body, "utf-8");// 解决中文乱码问题 entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httppost.setEntity(entity); } CloseableHttpResponse response = httpclient.execute(httppost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { result = EntityUtils.toString(httpEntity, "UTF-8"); } response.close(); httpclient.close(); System.out.println(result); return result; }
/** * 发送get 请求一个url返回 返回的内容 */ public static String get(String url,String token) throws Exception { String html = null; CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建httpClient实例 HttpGet httpGet = new HttpGet(url); // 创建httpget实例 // 设置请求头 httpGet.setHeader("appKey", PropertiesUtil.appKey); httpGet.setHeader("appSecret", PropertiesUtil.appSecret); httpGet.setHeader("Content-Type", "application/json"); if(StringUtil.isNotEmpty(token)){ httpGet.setHeader("openToken", token); } // 设置请求头 HttpResponse response = httpClient.execute(httpGet); // 执行http get请求 // 处理响应 int statusCode = response.getStatusLine().getStatusCode(); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("Status code: " + statusCode); System.out.println("Response body: " + responseBody); return responseBody; }
站长微信:xiaomao0055
站长QQ:14496453