1 /** 2 * *******************************傳統請求--開始************************************** 3 * */ 4 5 /** 6 * HttpClient 發送 Post 請求 7 * @param url 路徑:String 8 * @param mapParam 參數:java.util.Map 9 * @return 10 */ 11 public static String sendClientPost(String url,Map<String,String> headers,Map<String,String> mapParam){ 12 return httpPost(url, headers,new JSONObject(mapParam), false); 13 } 14 15 /** 16 * HttpClient 發送 Post 請求 17 * @param url 路徑:String 18 * @param JsonObjectParam 參數:org.json.JSONObject 19 * @return 20 */ 21 public static String sendClientPost(String url,Map<String,String> headers,JSONObject JsonObjectParam){ 22 return httpPost(url, headers,JsonObjectParam, false); 23 } 24 25 26 /** 27 * HttpClient 發送 Get 請求 28 * @param url 路徑:String 29 * @return 30 */ 31 public static String sendClientGet(String url){ 32 return httpGet(url,null); 33 } 34 /** 35 * HttpClient 發送 Get 請求 36 * @param url 路徑:String 37 * @param headers Map<String,String> 38 * @return 39 */ 40 public static String sendClientGet(String url,Map<String,String> headers){ 41 return httpGet(url,headers); 42 } 43 44 /** 45 * HttpClient 發送 Delete 請求 46 * @param url 路徑:String 47 * @return 48 */ 49 public static String sendClientDelete(String url){ 50 return httpDelete(url,null); 51 } 52 /** 53 * HttpClient 發送 Delete 請求 54 * @param url 路徑:String 55 * @param headers Map<String,String> 56 * @return 57 */ 58 public static String sendClientDelete(String url,Map<String,String> headers){ 59 return httpDelete(url,headers); 60 } 61 62 63 /** 64 * urlConnectionGet 65 * @param url 路徑 66 * @return 67 */ 68 public static String sendConnectionGet(String url,String param){ 69 return sendGet(url,param); 70 } 71 72 /** 73 * 向指定 URL 發送POST方法的請求 74 * @param url 發送請求的 URL 75 * @param param 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 76 * @return 所表明遠程資源的響應結果 77 */ 78 public static String sendConnectionPost(String url,String param){ 79 return sendPost(url,param); 80 } 81 /** 82 * *******************************傳統請求--結束************************************** 83 * */ 84 85 86 /** 87 * *******************************Jsoup請求--開始************************************** 88 * */ 89 90 /** 91 * 發送 POST 請求 92 * @param url 請求地址 93 * @param cookie cookie:String 94 * @param headers 頭信息:Map<String,String> 95 * @param params 參數:Map<String,Object> 96 * @return String 97 */ 98 public static String sendJsoupPost(String url,String cookie,Map<String,String> headers,Map<String,Object> params,boolean isJsonParam) { 99 String sendRequest = sendRequest(url, cookie, headers,params, "POST",isJsonParam); 100 return sendRequest; 101 } 102 103 /** 104 * 發送 POST 請求 105 * @param url 請求地址 106 * @param headers 頭信息:Map<String,String> 107 * @param params 參數:Map<String,Object> 108 * @return String 109 */ 110 public static String sendJsoupPost(String url,Map<String,String> headers,Map<String,Object> params,boolean isJsonParam) { 111 String sendRequest = sendRequest(url, null, headers,params, "POST", isJsonParam); 112 return sendRequest; 113 } 114 115 /** 116 * 發送 POST 請求 117 * @param url 請求地址 118 * @param cookie cookie:String 119 * @param headers 頭信息:Map<String,String> 120 * @param params 參數:Map<String,Object> 121 * @return String 122 */ 123 public static String sendJsoupPost(String url,Map<String,Object> params,boolean isJsonParam) { 124 String sendRequest = sendRequest(url, null, null,params, "POST", isJsonParam); 125 return sendRequest; 126 } 127 128 129 /** 130 * 發送 POST 請求 131 * @param url 請求地址 132 * @return String 133 */ 134 public static String sendJsoupPost(String url) { 135 String sendRequest = sendRequest(url, null, null,null, "POST",false); 136 return sendRequest; 137 } 138 139 140 /** 141 * 發送 GET 請求 142 * @param url 請求地址: String 143 * @param cookie cookie:String 144 * @param headers 頭信息:Map<String,String> 145 * @param params 參數:Map<String,Object> 146 * @return String 147 */ 148 public static String sendJsoupGet(String url,String cookie,Map<String,String> headers,Map<String,Object> params) { 149 String sendRequest = sendRequest(url, cookie, headers,params, "GET",false); 150 return sendRequest; 151 } 152 153 /** 154 * 發送 GET 請求 155 * @param url 請求地址: String 156 * @param headers 頭信息:Map<String,String> 157 * @param params 參數:Map<String,Object> 158 * @return String 159 */ 160 public static String sendJsoupGet(String url,Map<String,String> headers,Map<String,Object> params) { 161 String sendRequest = sendRequest(url, null, headers,params, "GET",false); 162 return sendRequest; 163 } 164 165 /** 166 * 發送 GET 請求 167 * @param url 請求地址: String 168 * @return String 169 */ 170 public static String sendJsoupGet(String url) { 171 String sendRequest = sendRequest(url, null, null,null, "GET",false); 172 return sendRequest; 173 } 174 175 /** 176 * 發送 GET 請求 177 * @param url 請求地址: String 178 * @param params 參數:Map<String,Object> 179 * @return String 180 */ 181 public static String sendJsoupGet(String url,Map<String,Object> params) { 182 String sendRequest = sendRequest(url, null, null,params, "GET",false); 183 return sendRequest; 184 } 185 186 /** 187 * *******************************Jsoup請求--結束************************************** 188 * */ 189 190 191 /** 192 * *******************************獲取網頁對象--開始************************************** 193 * */ 194 195 /** 196 * 發送 GET 請求 197 * @param url 請求地址: String 198 * @return Document 199 */ 200 public static Document sendJsoupGetDocument(String url) { 201 Document sendRequestDocument = sendRequestDocument(url, null, null,null, "GET",false); 202 return sendRequestDocument; 203 } 204 205 /** 206 * 發送 GET 請求 207 * @param url 請求地址: String 208 * @param cookie cookie:String 209 * @param headers 頭信息:Map<String,String> 210 * @param params 參數:Map<String,Object> 211 * @return Document 212 */ 213 public static Document sendJsoupGetDocument(String url,String cookie,Map<String,String> headers,Map<String,Object> params) { 214 Document sendRequest = sendRequestDocument(url, cookie, headers,params, "GET",false); 215 return sendRequest; 216 } 217 218 /** 219 * 發送 POST 請求 220 * @param url 請求地址: String 221 * @return Document 222 */ 223 public static Document sendJsoupPostDocument(String url) { 224 Document sendRequestDocument = sendRequestDocument(url, null, null,null, "POST",false); 225 return sendRequestDocument; 226 } 227 228 /** 229 * 發送 POST 請求 230 * @param url 請求地址 231 * @param cookie cookie:String 232 * @param headers 頭信息:Map<String,String> 233 * @param params 參數:Map<String,Object> 234 * @return Document 235 */ 236 public static Document sendJsoupPostDocument(String url,String cookie,Map<String,String> headers,Map<String,Object> params,boolean isJsonParam) { 237 Document sendRequest = sendRequestDocument(url, cookie, headers,params, "POST", isJsonParam); 238 return sendRequest; 239 } 240 241 242 /** 243 * *******************************獲取網頁對象--結束************************************** 244 * */ 245 246 247 248 249 250 251 252 /** 253 * post請求 254 * @param url url地址 255 * @param jsonParam 參數 256 * @param noNeedResponse 不須要返回結果 257 * @return 258 */ 259 private static String httpPost(String url,Map<String,String> headers,JSONObject jsonParam, boolean noNeedResponse){ 260 //post請求返回結果 261 CloseableHttpClient httpClient = HttpClients.createDefault(); 262 // DefaultHttpClient httpClient = new DefaultHttpClient(); 263 String jsonResult = null; 264 HttpPost method = new HttpPost(url); 265 266 if(headers!=null){ 267 Set<String> keySet = headers.keySet(); 268 for (String key : keySet) { 269 method.addHeader(key, headers.get(key)); 270 } 271 } 272 try { 273 if (null != jsonParam) { 274 //解決中文亂碼問題 275 StringEntity entity = new StringEntity(jsonParam.toString(), "UTF-8"); 276 entity.setContentEncoding("UTF-8"); 277 entity.setContentType("application/json"); 278 method.setEntity(entity); 279 } 280 HttpResponse result = httpClient.execute(method); 281 url = URLDecoder.decode(url, "UTF-8"); 282 /**請求發送成功,並獲得響應**/ 283 //if (result.getStatusLine().getStatusCode() == 200) { 284 try { 285 /**讀取服務器返回過來的json字符串數據**/ 286 jsonResult = EntityUtils.toString(result.getEntity()); 287 if (noNeedResponse) { 288 return null; 289 } 290 /**把json字符串轉換成json對象**/ 291 } catch (Exception e) { 292 logger.error("HttpPost請求提交失敗:" + url, e); 293 } 294 //} 295 } catch (Exception e) { 296 logger.error("HttpPost請求提交失敗:" + url, e); 297 } 298 return jsonResult; 299 } 300 301 302 /** 303 * 發送get請求 304 * @param url 路徑 305 * @return 306 */ 307 private static String httpGet(String url,Map<String,String> headers){ 308 //get請求返回結果 309 String jsonResult = null; 310 try { 311 CloseableHttpClient httpClient = HttpClients.createDefault(); 312 // DefaultHttpClient client = new DefaultHttpClient(); 313 //發送get請求 314 HttpGet request = new HttpGet(url); 315 if(headers!=null){ 316 Set<String> keySet = headers.keySet(); 317 for (String key : keySet) { 318 request.addHeader(key, headers.get(key)); 319 } 320 } 321 322 HttpResponse response = httpClient.execute(request); 323 324 /**請求發送成功,並獲得響應**/ 325 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 326 /**讀取服務器返回過來的json字符串數據**/ 327 jsonResult = EntityUtils.toString(response.getEntity()); 328 } else { 329 logger.error("HttpGet請求提交失敗:" + url); 330 } 331 } catch (Exception e) { 332 logger.error("HttpGet請求提交失敗:" + url, e); 333 } 334 return jsonResult; 335 } 336 337 /** 338 * 發送get請求 339 * @param url 路徑 340 * @return 341 */ 342 private static String httpDelete(String url,Map<String,String> headers){ 343 //get請求返回結果 344 String jsonResult = null; 345 try { 346 CloseableHttpClient httpClient = HttpClients.createDefault(); 347 // DefaultHttpClient client = new DefaultHttpClient(); 348 //發送get請求 349 HttpDelete request = new HttpDelete(url); 350 if(headers!=null){ 351 Set<String> keySet = headers.keySet(); 352 for (String key : keySet) { 353 request.addHeader(key, headers.get(key)); 354 } 355 } 356 357 HttpResponse response = httpClient.execute(request); 358 359 /**請求發送成功,並獲得響應**/ 360 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 361 /**讀取服務器返回過來的json字符串數據**/ 362 jsonResult = EntityUtils.toString(response.getEntity()); 363 } else { 364 logger.error("HttpDelete請求提交失敗:" + url); 365 } 366 } catch (Exception e) { 367 logger.error("HttpDelete請求提交失敗:" + url, e); 368 } 369 return jsonResult; 370 } 371 372 /** 373 * 向指定URL發送GET方法的請求 374 * @param url 發送請求的URL 375 * @param param 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 376 * @return URL 所表明遠程資源的響應結果 377 */ 378 private static String sendGet(String url, String param) { 379 380 String result = ""; 381 BufferedReader in = null; 382 try { 383 String urlNameString = url + "?" + param; 384 URL realUrl = new URL(urlNameString); 385 // 打開和URL之間的鏈接 386 URLConnection connection = realUrl.openConnection(); 387 // 設置通用的請求屬性 388 connection.setRequestProperty("accept", "*/*"); 389 connection.setRequestProperty("connection", "Keep-Alive"); 390 connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 391 // 創建實際的鏈接 392 connection.connect(); 393 // 獲取全部響應頭字段 394 // Map<String, List<String>> map = connection.getHeaderFields(); 395 // 遍歷全部的響應頭字段 396 // 定義 BufferedReader輸入流來讀取URL的響應 397 in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); 398 String line; 399 while ((line = in.readLine()) != null) { 400 result += line; 401 } 402 } catch (Exception e) { 403 System.out.println("發送GET請求出現異常!" + e); 404 e.printStackTrace(); 405 } 406 // 使用finally塊來關閉輸入流 407 finally { 408 try { 409 if (in != null) { 410 in.close(); 411 } 412 } catch (Exception e2) { 413 e2.printStackTrace(); 414 } 415 } 416 return result; 417 } 418 419 420 /** 421 * 向指定 URL 發送POST方法的請求 422 * @param url 發送請求的 URL 423 * @param param 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 424 * @return 所表明遠程資源的響應結果 425 */ 426 private static String sendPost(String url, String param) { 427 428 PrintWriter out = null; 429 BufferedReader in = null; 430 String result = ""; 431 try { 432 URL realUrl = new URL(url); 433 // 打開和URL之間的鏈接 434 URLConnection conn = realUrl.openConnection(); 435 // 設置通用的請求屬性 436 conn.setRequestProperty("accept", "*/*"); 437 conn.setRequestProperty("connection", "Keep-Alive"); 438 conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 439 // 發送POST請求必須設置以下兩行 440 conn.setDoOutput(true); 441 conn.setDoInput(true); 442 // 獲取URLConnection對象對應的輸出流 443 out = new PrintWriter(conn.getOutputStream()); 444 // 發送請求參數 445 out.print(param); 446 // flush輸出流的緩衝 447 out.flush(); 448 // 定義BufferedReader輸入流來讀取URL的響應 449 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 450 String line; 451 while ((line = in.readLine()) != null) { 452 result += line; 453 } 454 } catch (Exception e) { 455 System.out.println("發送 POST 請求出現異常!"+e); 456 e.printStackTrace(); 457 }finally{ 458 try{ 459 if(out!=null){ 460 out.close(); 461 } 462 if(in!=null){ 463 in.close(); 464 } 465 }catch(Exception ex){ 466 ex.printStackTrace(); 467 } 468 } 469 return result; 470 } 471 472 /** 發送 利用JSOUP 發送 Request 請求 473 * @param url 請求地址: String 474 * @param cookie cookie:String 475 * @param headers 頭信息:Map<String,String> 476 * @param params 參數:Map<String,Object> 477 * @param method 請求方式: String,GER OR POST 478 * @return String 479 */ 480 private static String sendRequest(String url,String cookie,Map<String,String> headers,Map<String,Object> params,String method,boolean isJsonParam) { 481 String res = null; 482 try { 483 Document document = null; 484 Connection connect = getConnection(url, cookie, headers, params,isJsonParam); 485 connect.ignoreContentType(true); 486 if("GET".equals(method)) { 487 document = connect.get(); 488 }else if("POST".equals(method)) { 489 document = connect.post(); 490 } 491 492 if(document != null) { 493 res = document.body().text(); 494 } 495 } catch (Exception e) { 496 e.printStackTrace(); 497 } 498 return res; 499 } 500 501 502 /** 發送 利用JSOUP 發送 Request 請求 503 * @param url 請求地址: String 504 * @param cookie cookie:String 505 * @param headers 頭信息:Map<String,String> 506 * @param params 參數:Map<String,Object> 507 * @param method 請求方式: String,GER OR POST 508 * @return String 509 */ 510 private static Document sendRequestDocument(String url,String cookie,Map<String,String> headers,Map<String,Object> params,String method,boolean isJsonParam) { 511 Document document = null; 512 try { 513 Connection connect = getConnection(url, cookie, headers, params,isJsonParam); 514 connect.ignoreContentType(true); 515 if("GET".equals(method)) { 516 document = connect.get(); 517 }else if("POST".equals(method)) { 518 document = connect.post(); 519 } 520 } catch (Exception e) { 521 e.printStackTrace(); 522 } 523 return document; 524 } 525 526 /** 527 * 獲取JSOUP Connection 528 * @param url 請求地址: String 529 * @param cookie cookie:String 530 * @param headers 頭信息:Map<String,String> 531 * @param params 頭信息:Map<String,Object> 532 * @return Connection org.jsoup.Connection 533 */ 534 private static Connection getConnection(String url,String cookie,Map<String,String> headers,Map<String,Object> params,boolean isJsonParam) { 535 Connection connect = Jsoup.connect(url); 536 if(cookie != null) { 537 connect.header("Cookie", cookie); 538 } 539 540 if(params != null) { 541 Map<String,String> param = new HashMap<String,String>(); 542 Set<String> keySet = params.keySet(); 543 for (String key : keySet) { 544 if(params.get(key) != null){ 545 param.put(key, params.get(key)==null ? null : params.get(key)+""); 546 } 547 } 548 if(isJsonParam){ 549 connect.requestBody(new JSONObject(param).toString()); 550 // connect.postDataCharset(new JSONObject(param).toString()); 551 }else{ 552 553 connect.data(param); 554 } 555 } 556 557 if(headers!= null) { 558 connect.headers(headers); 559 }else if(headers==null) { 560 connect.header("Accept", "application/json"); 561 connect.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0"); 562 } 563 return connect; 564 } 565