WebUtils.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package com.luooqi.ocr.utils;
  2. import java.util.Hashtable;
  3. import java.util.Map;
  4. import cn.hutool.http.Header;
  5. import cn.hutool.http.HttpRequest;
  6. import cn.hutool.http.HttpResponse;
  7. import cn.hutool.http.HttpUtil;
  8. import cn.hutool.log.StaticLog;
  9. /**
  10. * fish-web
  11. * Created by 何志龙 on 2018-03-25.
  12. */
  13. @SuppressWarnings("SpellCheckingInspection")
  14. public class WebUtils {
  15. static {
  16. HttpRequest.closeCookie();
  17. }
  18. public static String getSafeHtml(HttpResponse response) {
  19. if (response == null) {
  20. return "";
  21. }
  22. return response.body();
  23. }
  24. public static String getHtml(String url) {
  25. HttpResponse response = get(url);
  26. String html = getSafeHtml(response);
  27. if (response != null) {
  28. response.close();
  29. }
  30. return html;
  31. }
  32. public static HttpResponse get(String url) {
  33. return get(url, 0, null, true);
  34. }
  35. public static String getLocation(String url, String cookie) {
  36. try {
  37. HttpResponse response = get(url, 0, new Hashtable<String, String>() {{
  38. put("Cookie", cookie);
  39. }}, false);
  40. if (response == null) {
  41. return url;
  42. }
  43. String location = response.header(Header.LOCATION);
  44. response.close();
  45. return location;
  46. } catch (Exception ex) {
  47. return "";
  48. }
  49. }
  50. public static HttpResponse get(String url, String cookie) {
  51. return get(url, 0, new Hashtable<String, String>() {{
  52. put("Cookie", cookie);
  53. }}, true);
  54. }
  55. public static HttpResponse get(String url, int userAgent, String cookie) {
  56. return get(url, userAgent, new Hashtable<String, String>() {{
  57. put("Cookie", cookie);
  58. }}, true);
  59. }
  60. public static HttpResponse get(String url, int userAgent, Map<String, String> headers) {
  61. return get(url, userAgent, headers, true);
  62. }
  63. public static HttpResponse get(String url, int userAgent, Map<String, String> headers, boolean allowRedirct) {
  64. try {
  65. HttpRequest request = HttpUtil.createGet(url).timeout(10000).setFollowRedirects(allowRedirct);
  66. if (headers == null) {
  67. headers = new Hashtable<>();
  68. }
  69. switch (userAgent) {
  70. case 1:
  71. headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69 MicroMessenger/6.3.16 NetType/WIFI Language/zh_CN");
  72. break;
  73. case 2:
  74. headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
  75. break;
  76. case 3:
  77. headers.put("User-Agent", "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; NOKIA; Lumia 930) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/13.10586");
  78. break;
  79. case 4:
  80. headers.put("User-Agent", "NativeHost");
  81. break;
  82. case 5:
  83. headers.put("User-Agent", "Dalvik/1.6.0 (Linux; U; Android 4.4.2; NoxW Build/KOT49H) ITV_5.7.1.46583");
  84. break;
  85. case 6:
  86. headers.put("User-Agent", "qqlive");
  87. break;
  88. case 7:
  89. headers.put("User-Agent", "Dalvik/1.6.0 (Linux; U; Android 4.2.2; 6S Build/JDQ39E)");
  90. break;
  91. case 8:
  92. headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) XIAMI-MUSIC/3.0.9 Chrome/56.0.2924.87 Electron/1.6.11 Safari/537.36");
  93. break;
  94. case 9:
  95. headers.put("User-Agent", "okhttp/2.7.5");
  96. break;
  97. case 10:
  98. headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; oppo r11 plus Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 SogouSearch Android1.0 version3.0");
  99. break;
  100. default:
  101. headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
  102. break;
  103. }
  104. request.addHeaders(headers);
  105. return request.execute();
  106. } catch (Exception ex) {
  107. StaticLog.error(ex);
  108. return null;
  109. }
  110. }
  111. public static HttpResponse postRaw(String url, String data) {
  112. return postRaw(url, data, 0, null);
  113. }
  114. public static HttpResponse postRaw(String url, String data, int userAgent, Map<String, String> headers) {
  115. return postData(url, new Hashtable<String, Object>() {{
  116. put("FORM", data);
  117. }}, 2, userAgent, headers);
  118. }
  119. public static HttpResponse postJson(String url, String data, int userAgent, Map<String, String> headers) {
  120. return postData(url, new Hashtable<String, Object>() {{
  121. put("JSON", data);
  122. }}, 1, userAgent, headers);
  123. }
  124. public static HttpResponse postForm(String url, Map<String, Object> data, int userAgent, Map<String, String> headers) {
  125. return postData(url, data, 0, userAgent, headers);
  126. }
  127. private static HttpResponse postData(String url, Map<String, Object> data, int contentType, int userAgent, Map<String, String> headers) {
  128. try {
  129. HttpRequest request = HttpUtil.createPost(url).timeout(10000);
  130. if (contentType == 0) {
  131. request.contentType("application/x-www-form-urlencoded");
  132. request.form(data);
  133. } else if (contentType == 1) {
  134. request.body(data.values().iterator().next().toString(), "application/json;charset=UTF-8");
  135. } else {
  136. request.contentType("application/x-www-form-urlencoded");
  137. request.body(data.values().iterator().next().toString());
  138. }
  139. if (headers == null) {
  140. headers = new Hashtable<>();
  141. }
  142. switch (userAgent) {
  143. case 1:
  144. headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3");
  145. break;
  146. case 2:
  147. headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
  148. break;
  149. case 3:
  150. headers.put("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)");
  151. break;
  152. case 4:
  153. headers.put("User-Agent", "NativeHost");
  154. break;
  155. case 5:
  156. headers.put("User-Agent", "Apache-HttpClient/UNAVAILABLE (java 1.4)");
  157. break;
  158. case 6:
  159. headers.put("User-Agent", "Mozilla/5.0 (iPad; CPU OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B466 Safari/600.1.4");
  160. break;
  161. case 7:
  162. headers.put("User-Agent", "okhttp/2.7.5");
  163. break;
  164. case 10:
  165. headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; oppo r11 plus Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 SogouSearch Android1.0 version3.0");
  166. break;
  167. default:
  168. headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
  169. break;
  170. }
  171. request.addHeaders(headers);
  172. return request.execute();
  173. } catch (Exception ex) {
  174. StaticLog.error(ex);
  175. return null;
  176. }
  177. }
  178. }