import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import javax.annotation.Resource; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; / * 模拟浏览器请求任何一个网址 * @author */ @Component("httpUtil") public class HttpUtil {
/* 文件工具类 */ @Resource private FileUtil fileUtil; public class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
// 直接返回true return true; } } / * 输入一个url, * 返回这个url对应的html代码 * * @param url:网址 * @return 返回网址对应的html代码 */ public static String methodGet(String urlStr) {
//ConstatFinalUtil.SYS_LOGGER.info("--methodGet--url:{}",urlStr); StringBuffer sb = new StringBuffer() ; try {
URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); /* 获取网址对应的输入流和输出流 */ InputStream is = connection.getInputStream() ; BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); String line = "" ; while((line = br.readLine()) != null) {
sb.append(line); } } catch (Exception e) {
e.printStackTrace(); } return sb.toString() ; } / * 输入一个url, * 返回这个url对应的html代码 * * @param url:网址 * @param paramsMap 提交参数;键=值 * @return 返回网址对应的html代码 */ public String methodPost(String urlStr,Map<String, String> paramsMap) {
//ConstatFinalUtil.SYS_LOGGER.info("--methodGet--url:{}",urlStr); StringBuffer sb = new StringBuffer() ; try {
URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection() ; /* Post请求,必须加以下操作 */ connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); /* 获取网址对应的输入流和输出流 */ OutputStream os = connection.getOutputStream() ; BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); /* 先向服务器写数据 * 如何将我们的参数写到输出流呢??? * 得遵循HTTP协议,看浏览器怎么做,我们也怎么做 * * URL get中的字符串 * method=submit&email=aaa&password=bbbb * */ StringBuffer paramSb = new StringBuffer() ; for (Iterator iterator = paramsMap.entrySet().iterator(); iterator.hasNext();) {
Entry me = (Entry) iterator.next(); String key = me.getKey() + ""; String value = me.getValue() + "" ; paramSb.append(key + "=" + value + "&"); } /* * method=submit&email=aaa&password=bbbb * email=bb&method=submit&password=cc * */ bw.write(paramSb.toString()); bw.flush(); bw.close(); InputStream is = connection.getInputStream() ; BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); String line = "" ; while((line = br.readLine()) != null) {
sb.append(line); } br.close(); } catch (Exception e) {
e.printStackTrace(); } return sb.toString() ; } public static void main(String[] args) {
HttpUtil httpUtil = new HttpUtil() ; /*String url = "http://c.v..com/vchannelinfo?otype=json&uin=d32c9189aaabe3c6c6aff3832c&qm=1&pagenum=3&num=10&sorttype=0&orderflag=0&callback=jQuery_45&low_login=1&_=56" ; String res = httpUtil.methodGet(url); System.out.println(res);*/ String ceshString ="{json={\"method\":\"verifyAdminsAuth\",\"data\":{\"currentUrl\":\"/carConAll-web-back/back/vehicle/vehHistoryList.htm?from=home\",\"adminsId\":2},\"encrypt\":\"e12299c068c5e8dd3a1e30ed9780b82721940a7779abfeaecbb409a9466fd673\",\"version\":\"1\",\"pubKey\":\"\"}}"; JSONObject parse = (JSONObject) JSON.parse(ceshString); /* * String url = "http://localhost:10001/shop-web-head/insert.jsp" ; * Map
paramsMap = new HashMap
(); * paramsMap.put("email", "bb"); paramsMap.put("password", "cc"); * paramsMap.put("method", "submit"); String res = * httpUtil.methodPost(url,paramsMap); */
System.out.println(parse); } / * 下载文件 */ public boolean downLoadFile(Map<String, String> headerMap,Map<String, String> paramsMap,OutputStream os) {
HttpURLConnection connect = null ; try {
URL url = new URL(paramsMap.get("requestURL")); connect = (HttpURLConnection) url .openConnection(); connect.setConnectTimeout(ConstatFinalUtil.REQ_CONNECT_TIMEOUT); connect.setReadTimeout(ConstatFinalUtil.READ_TIMEOUT); StringBuffer paramssb = new StringBuffer(); for (Iterator iterator = paramsMap.entrySet().iterator(); iterator .hasNext();) {
Map.Entry me = (Map.Entry) iterator.next(); if (!"requestURL".equalsIgnoreCase(me.getKey() + "") && !"retry".equalsIgnoreCase(me.getKey() + "")) {
paramssb.append(me.getKey() + "=" + me.getValue() + "&"); } } if (paramssb.toString().endsWith("&")) {
paramssb.delete(paramssb.length() - 1, paramssb.length()); } connect.setRequestProperty( "Cookie", "Hm_lvt_2d57a0f88eed9744a82604dcfa102e49=; CNZZDATA=cnzz_eid%3D--http%253A%252F%252Fwww.btctrade.com%26ntime%3D%26cnzz_a%3D5%26ltime%3D33%26rtime%3D1; pgv_pvi=; __utma=.....3; __utmz=..1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); wafenterurl=/; wafcookie=51a45aee92bbd8cc281e98e91; __utmb=.1.10.; __utmc=; wafverify=afe13eda6d99c7f141d7dd3966b59d9e; USER_PW=ab3f61ee826a95e51734cf; PHPSESSID=9f2c19d6ffd0ef808ba8bac0b74ab0f3; IESESSION=alive; pgv_si=s"); connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:25.0) Gecko/ Firefox/25.0"); for (Iterator iterator = headerMap.entrySet().iterator(); iterator .hasNext();) {
Entry me = (Entry) iterator.next(); connect.setRequestProperty(me.getKey() + "", me.getValue() + ""); } InputStream is = connect.getInputStream() ; return this.fileUtil.copyFile(is, os); }catch (Exception e) {
try {
ConstatFinalUtil.SYS_LOGGER.info("http的Post请求失败了;响应码:{};请求头:{};请求体:{}", connect != null ? connect.getResponseCode() : connect,headerMap,paramsMap,e); Thread.sleep(ConstatFinalUtil.PAGE_BATCH_SIZE); } catch (Exception e1) {
e1.printStackTrace(); } } return false; } / * 上传文件 * @param requestMap 双传文件的信息 * @param filePath * @return */ public String methodUploadFile(Map<String, String> headerMap,Map<String, String> requestMap) {
String urlStr = headerMap.get("requestURL"); if(!urlStr.endsWith("?")) {
urlStr = urlStr + "?" ; } for (Iterator iterator = requestMap.entrySet().iterator(); iterator.hasNext();) {
Entry me = (Entry) iterator.next(); String key = me.getKey() + "" ; String val = me.getValue() + "" ; urlStr = urlStr + "&" + key + "=" + val ; } /* 返回值 */ StringBuffer sbRes = new StringBuffer() ; String filePath = requestMap.get("filePath"); try {
// 换行符 final String newLine = "\r\n"; final String boundaryPrefix = "--"; // 定义数据分隔线 String BOUNDARY = "========7d4a6d158c9"; // 服务器的域名 URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置为POST请求 conn.setRequestMethod("POST"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); // 设置请求头参数 conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream()); // 上传文件 File file = new File(filePath); StringBuilder sb = new StringBuilder(); sb.append(boundaryPrefix); sb.append(BOUNDARY); sb.append(newLine); // 文件参数,photo参数名可以随意修改 sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + filePath + "\"" + newLine); sb.append("Content-Type:application/octet-stream"); // 参数头设置完以后需要两个换行,然后才是参数内容 sb.append(newLine); sb.append(newLine); // 将参数头的数据写入到输出流中 out.write(sb.toString().getBytes()); // 数据输入流,用于读取文件数据 DataInputStream in = new DataInputStream(new FileInputStream(file)); byte[] bufferOut = new byte[1024]; int bytes = 0; // 每次读1KB数据,并且将文件数据写入到输出流中 while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes); } // 最后添加换行 out.write(newLine.getBytes()); in.close(); // 定义最后数据分隔线,即--加上BOUNDARY再加上--。 byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine).getBytes(); // 写上结尾标识 out.write(end_data); out.flush(); out.close(); // 定义BufferedReader输入流来读取URL的响应 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) {
sbRes.append(line); } } catch (Exception e) {
ConstatFinalUtil.SYS_LOGGER.error("上传文件失败了;文件名:{}",filePath , e); } return sbRes.toString() ; } / * Post请求 * * @param headerMap * 请求头 * @param paramsMap * 请求体参数 * @return */ public String methodPost(Map<String, String> headerMap, Map<String, String> paramsMap) {
StringBuffer sb = new StringBuffer(); //如果是https协议 if(paramsMap.get("requestURL").startsWith("https")) {
return this.methodHttpsPost(headerMap, paramsMap); } // 连续请求多次 for (int i = 0; i < ConstatFinalUtil.REQ_COUNT; i++) {
sb.delete(0, sb.length()); HttpURLConnection connect = null ; try {
URL url = new URL(paramsMap.get("requestURL")); connect = (HttpURLConnection) url .openConnection(); connect.setConnectTimeout(ConstatFinalUtil.REQ_CONNECT_TIMEOUT); connect.setReadTimeout(ConstatFinalUtil.READ_TIMEOUT); connect.setDoInput(true); connect.setDoOutput(true); StringBuffer paramssb = new StringBuffer(); for (Iterator iterator = paramsMap.entrySet().iterator(); iterator .hasNext();) {
Map.Entry me = (Map.Entry) iterator.next(); if (!"requestURL".equalsIgnoreCase(me.getKey() + "") && !"retry".equalsIgnoreCase(me.getKey() + "")) {
paramssb.append(me.getKey() + "=" + me.getValue() + "&"); } } if (paramssb.toString().endsWith("&")) {
paramssb.delete(paramssb.length() - 1, paramssb.length()); } connect.setRequestProperty( "Cookie", "Hm_lvt_2d57a0f88eed9744a82604dcfa102e49=; CNZZDATA=cnzz_eid%3D--http%253A%252F%252Fwww.btctrade.com%26ntime%3D%26cnzz_a%3D5%26ltime%3D33%26rtime%3D1; pgv_pvi=; __utma=.....3; __utmz=..1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); wafenterurl=/; wafcookie=51a45aee92bbd8cc281e98e91; __utmb=.1.10.; __utmc=; wafverify=afe13eda6d99c7f141d7dd3966b59d9e; USER_PW=ab3f61ee826a95e51734cf; PHPSESSID=9f2c19d6ffd0ef808ba8bac0b74ab0f3; IESESSION=alive; pgv_si=s"); connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:25.0) Gecko/ Firefox/25.0"); for (Iterator iterator = headerMap.entrySet().iterator(); iterator .hasNext();) {
Entry me = (Entry) iterator.next(); connect.setRequestProperty(me.getKey() + "", me.getValue() + ""); } BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( connect.getOutputStream())); bw.write(paramssb.toString()); bw.flush(); BufferedReader br = null ; //响应码成功 if(connect.getResponseCode() == 200) {
br = new BufferedReader(new InputStreamReader( connect.getInputStream(), "UTF-8")); }else {
if(connect.getErrorStream() != null) {
br = new BufferedReader(new InputStreamReader( connect.getErrorStream(), "UTF-8")); }else {
br = new BufferedReader(new InputStreamReader( connect.getInputStream(), "UTF-8")); } } String line = ""; while ((line = br.readLine()) != null) {
sb.append(line.trim()); } br.close(); bw.close(); //正常返回直接退出 if(connect.getResponseCode() != 200) {
ConstatFinalUtil.SYS_LOGGER.info("重试次数:{},响应码:{};响应信息:{};返回信息:{};请求头:{};请求体:{}", i,connect.getResponseCode(),connect.getResponseMessage(),sb,headerMap,paramsMap); } break ; }catch (Exception e) {
try {
ConstatFinalUtil.SYS_LOGGER.info("http的Post请求失败了;重试次数:{},响应码:{};返回信息:{};请求头:{};请求体:{}", i,connect != null ? connect.getResponseCode() : connect,sb,headerMap,paramsMap,e); Thread.sleep(ConstatFinalUtil.PAGE_BATCH_SIZE); } catch (Exception e1) {
e1.printStackTrace(); } } //重试机制 if(!"true".equalsIgnoreCase(headerMap.get("retry"))) {
break ; } } //没有查询到数据,调用https if(sb.length() <= 0) {
return this.methodHttpsPost(headerMap, paramsMap); } return sb.toString(); } / * HttpsPost请求 * * @param headerMap * 请求头参数 * @param paramsMap * 请求体参数 * @return */ public String methodHttpsPost(Map<String, String> headerMap, Map<String, String> paramsMap) {
StringBuffer sb = new StringBuffer(); // 连续请求多次 for (int i = 0; i < ConstatFinalUtil.REQ_COUNT; i++) {
sb.delete(0, sb.length()); HttpsURLConnection connect = null ; try {
// Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) {
} public void checkServerTrusted(X509Certificate[] certs, String authType) {
} } }; // Install the all-trusting trust manager SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection .setDefaultSSLSocketFactory(sc.getSocketFactory()); URL url = new URL(paramsMap.get("requestURL")); connect = (HttpsURLConnection) url .openConnection(); connect.setHostnameVerifier(new TrustAnyHostnameVerifier()); connect.setConnectTimeout(ConstatFinalUtil.REQ_CONNECT_TIMEOUT); connect.setReadTimeout(ConstatFinalUtil.READ_TIMEOUT); connect.setDoInput(true); connect.setDoOutput(true); connect.setRequestProperty( "Cookie", "Hm_lvt_2d57a0f88eed9744a82604dcfa102e49=; CNZZDATA=cnzz_eid%3D--http%253A%252F%252Fwww.btctrade.com%26ntime%3D%26cnzz_a%3D5%26ltime%3D33%26rtime%3D1; pgv_pvi=; __utma=.....3; __utmz=..1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); wafenterurl=/; wafcookie=51a45aee92bbd8cc281e98e91; __utmb=.1.10.; __utmc=; wafverify=afe13eda6d99c7f141d7dd3966b59d9e; USER_PW=ab3f61ee826a95e51734cf; PHPSESSID=9f2c19d6ffd0ef808ba8bac0b74ab0f3; IESESSION=alive; pgv_si=s"); connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:25.0) Gecko/ Firefox/25.0"); StringBuffer paramssb = new StringBuffer(); for (Iterator iterator = paramsMap.entrySet().iterator(); iterator .hasNext();) {
Map.Entry me = (Map.Entry) iterator.next(); if (!"requestURL".equalsIgnoreCase(me.getKey() + "")) {
paramssb.append(me.getKey() + "=" + me.getValue() + "&"); } } if (paramssb.toString().endsWith("&")) {
paramssb.delete(paramssb.length() - 1, paramssb.length()); } // ConstatFinalUtil.LOGGER.info("-----"+ paramssb); for (Iterator iterator = headerMap.entrySet().iterator(); iterator .hasNext();) {
Entry me = (Entry) iterator.next(); if (!"requestURL".equalsIgnoreCase(headerMap.get("requestURL"))) {
connect.setRequestProperty(me.getKey() + "", me.getValue() + ""); } } connect.setRequestProperty( "Cookie", "Hm_lvt_2d57a0f88eed9744a82604dcfa102e49=; CNZZDATA=cnzz_eid%3D--http%253A%252F%252Fwww.btctrade.com%26ntime%3D%26cnzz_a%3D5%26ltime%3D33%26rtime%3D1; pgv_pvi=; __utma=.....3; __utmz=..1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); wafenterurl=/; wafcookie=51a45aee92bbd8cc281e98e91; __utmb=.1.10.; __utmc=; wafverify=afe13eda6d99c7f141d7dd3966b59d9e; USER_PW=ab3f61ee826a95e51734cf; PHPSESSID=9f2c19d6ffd0ef808ba8bac0b74ab0f3; IESESSION=alive; pgv_si=s"); connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:25.0) Gecko/ Firefox/25.0"); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( connect.getOutputStream())); bw.write(paramssb.toString()); bw.flush(); BufferedReader br = null ; //响应码成功 if(connect.getResponseCode() == 200) {
br = new BufferedReader(new InputStreamReader( connect.getInputStream(), "UTF-8")); }else {
if(connect.getErrorStream() != null) {
br = new BufferedReader(new InputStreamReader( connect.getErrorStream(), "UTF-8")); }else {
br = new BufferedReader(new InputStreamReader( connect.getInputStream(), "UTF-8")); } } String line = ""; while ((line = br.readLine()) != null) {
sb.append(line.trim()); } br.close(); bw.close(); //正常返回直接退出 if(connect.getResponseCode() != 200) {
ConstatFinalUtil.SYS_LOGGER.info("重试次数:{},响应码:{};响应信息:{};返回信息:{};请求头:{};请求体:{}", i,connect.getResponseCode(),connect.getResponseMessage(),sb,headerMap,paramsMap); } break ; } catch (Exception e) {
try {
ConstatFinalUtil.SYS_LOGGER.info("https的Post请求失败了;重试次数:{},响应码:{};返回信息:{};请求头:{};请求体:{}", i,connect != null ? connect.getResponseCode() : connect,sb,headerMap,paramsMap,e); Thread.sleep(ConstatFinalUtil.PAGE_BATCH_SIZE); } catch (Exception e1) {
e1.printStackTrace(); } } //重试机制 if(!"true".equalsIgnoreCase(headerMap.get("retry"))) {
break ; } } return sb.toString(); } //新增 百度AI api图片文字识别请求方法 public static String post(String requestUrl, String accessToken, String params) throws Exception {
String contentType = "application/x-www-form-urlencoded"; return HttpUtil.post(requestUrl, accessToken, contentType, params); } public static String post(String requestUrl, String accessToken, String contentType, String params) throws Exception {
String encoding = "UTF-8"; if (requestUrl.contains("nlp")) {
encoding = "GBK"; } return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding); } public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding) throws Exception {
String url = requestUrl + "?access_token=" + accessToken; return HttpUtil.postGeneralUrl(url, contentType, params, encoding); } public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding) throws Exception {
URL url = new URL(generalUrl); // 打开和URL之间的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); // 设置通用的请求属性 connection.setRequestProperty("Content-Type", contentType); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setUseCaches(false); connection.setDoOutput(true); connection.setDoInput(true); // 得到请求的输出流对象 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(params.getBytes(encoding)); out.flush(); out.close(); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> headers = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : headers.keySet()) {
System.err.println(key + "--->" + headers.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 BufferedReader in = null; in = new BufferedReader( new InputStreamReader(connection.getInputStream(), encoding)); String result = ""; String getLine; while ((getLine = in.readLine()) != null) {
result += getLine; } in.close(); System.err.println("result:" + result); return result; } }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216466.html原文链接:https://javaforall.net
