WebUtil.java 24.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
package com.topdraw.platform.util;


import lombok.extern.slf4j.Slf4j;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 通过HTTP协议 访问服务器
 *
 * @author LucaMa
 *
 */
@Slf4j
public class WebUtil {
	public static boolean LOG = false;

	/**
	 * 向指定URL发送GET方法的请求
	 *
	 * @param url
	 *            发送请求的URL
	 * @param param
	 *            请求参数,请求参数应该是name1=value1&name2=value2的形式。
	 * @return URL所代表远程资源的响应
	 */
	public static String sendGet(String url, boolean needRedirect) {
		return sendGet(url, null, "UTF-8", null, needRedirect);
	}

	public static String sendGet(String url, String param, String charset) {
		return sendGet(url, param, charset, null, true);
	}

	public static String sendGet(String url, String param, String charset, Integer timeout) {
		return sendGet(url, param, charset, timeout, timeout, true);
	}

	public static String sendGet(String url, String param, String charset, Integer timeout, boolean needRedirect) {
		return sendGet(url, param, charset, timeout, timeout, needRedirect);
	}

	public static String sendGet(String url, String param, String charset, Integer connectTimeout,
			Integer readTimeout) {
		return sendGet(url, param, charset, connectTimeout, readTimeout, true);
	}

	public static String sendGet(String url, String param, String charset, Integer connectTimeout,Integer readTimeout,boolean needRedirect) {
		String result = "";
		StringBuilder sbResult = new StringBuilder();
		BufferedReader in = null;
		try {
			String urlName = url + ((null != param && !"".equals(param)) ? ("?" + param) : "");
			URL realUrl = new URL(urlName);
			// 打开和URL之间的连接
			HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
			// 设置通用的请求属性
			conn.setInstanceFollowRedirects(needRedirect);
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("charset", charset);
			if (null != connectTimeout && connectTimeout > 0) {
				conn.setConnectTimeout(connectTimeout);
			}
			if (null != readTimeout && readTimeout > 0) {
				conn.setReadTimeout(readTimeout);
			}
			//conn.setRequestProperty("charset", conn.setRequestProperty("charset", charset);;);
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			// 建立实际的连接
			conn.connect();
			// 获取所有响应头字段
			Map<String, List<String>> map = conn.getHeaderFields();
			// 遍历所有的响应头字段
			for (String key : map.keySet()) {
				if(LOG){
					log.info(key + "--->" + map.get(key));
				}
			}
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(
					new InputStreamReader(conn.getInputStream(),charset));
			String line;
			while ((line = in.readLine()) != null) {
				sbResult.append(line).append("\n");
			}
			if (null != sbResult && sbResult.length() > 0) {
				sbResult.setLength(sbResult.length() - 1);
			}
			result = sbResult.toString();
		} catch (Exception e) {
			//System.out.println("发送GET请求出现异常!" + e);
			e.printStackTrace();
			//throw e;
		}
		// 使用finally块来关闭输入流
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}


	public static String sendGet(String url, Map<String,String> mapParam , String charset) {
		StringBuilder sbParam=new StringBuilder("");
		int i1=0;
		Set<String> setKey=mapParam.keySet();
		for (String strKey : setKey ) {

		    sbParam.append(strKey).append("=").append(mapParam.get(strKey));
		    if (i1<setKey.size()-1){
		    	sbParam.append("&");
		    }
		    i1++;
		}
		return sendGet(url, sbParam.toString(), charset);
	}


	/**
	 * 向指定URL发送POST方法的请求
	 *
	 * @param url
	 *            发送请求的URL
	 * @param param
	 *            请求参数,请求参数应该是name1=value1&name2=value2的形式。
	 * @return URL所代表远程资源的响应
	 */
	public static String sendPost(String url, String param , String charset, String contentType) {
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";
		StringBuilder sbResult = new StringBuilder();
		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("charset", charset);

			if (null!=contentType && !contentType.isEmpty()){
				conn.setRequestProperty("Content-Type",contentType);
			}
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(conn.getOutputStream());


			// 发送请求参数
			out.print(new String(param.getBytes(charset)));
			// flush 输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(
					new InputStreamReader(conn.getInputStream(),charset));
			String line;
			while ((line = in.readLine()) != null) {
				sbResult.append(line).append("\n");
			}
			if (null != sbResult && sbResult.length() > 0) {
				sbResult.setLength(sbResult.length() - 1);
			}
			result = sbResult.toString();
		} catch (Exception e) {
			//System.out.println("发送POST 请求出现异常!" + e);
			e.printStackTrace();

		}
		// 使用finally块来关闭输出流、输入流.
		finally {

			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

	public static String sendPost(String url, Map<String,String> mapParam , String charset,String contentType) {

		StringBuilder sbParam=new StringBuilder("");
		int i1=0;
		Set<String> setKey=mapParam.keySet();
		for (String strKey : setKey ) {

		    sbParam.append(strKey).append("=").append(mapParam.get(strKey));
		    if (i1<setKey.size()-1){
		    	sbParam.append("&");
		    }
		    i1++;
		}

		return sendPost(url, sbParam.toString(), charset,contentType);
	}






//////////////////////////////////////////////////以下新版////////////////////////////////////////////





	/**
	 *  新版本的 GET请求
	 *
	 * @param strURL
	 *            发送请求的URL
	 * @param strParam
	 *            请求参数,应该是name1=value1&name2=value2的形式。
	 * @thows Exception
	 *
	 * @return URL所代表远程资源的响应
	 */
	public static String sendGet(String strURL, String strParam, Map<String,String> mapHeader ) throws Exception {

		String strResult = "";
		BufferedReader brIn = null;
		try {
			String strFullURL = strURL + (null != strParam ? ("?" + strParam) : "");
			URL url = new URL(strFullURL);
			// 打开和URL之间的连接
			//URLConnection conn = urlReal.openConnection();
			HttpURLConnection connHttps = (HttpURLConnection)url.openConnection();

			connHttps.setRequestMethod("GET");

			connHttps.setUseCaches(false);

			// 设置通用的请求属性
			connHttps.setRequestProperty("Accept", "*/*");
			connHttps.setRequestProperty("Charset", "UTF-8");
			connHttps.setRequestProperty("Connection", "Keep-Alive");
			connHttps.setRequestProperty("User-Agent", "Server(Java) WebUtil");

			if (null!=mapHeader && !mapHeader.isEmpty()){
				for(String strKey:mapHeader.keySet()){
					String strValue=mapHeader.get(strKey);

					char cFirst = strKey.charAt(0);
					if(cFirst < 65 || cFirst > 90){ //不是大写开头
						//System.out.println( new StringBuilder("A http header parameter '").append(strKey).append("' isn't start with capital letter.."));
					}
					if (null!=strValue && !strValue.isEmpty()){
						connHttps.setRequestProperty(strKey, strValue);

					}
				}
			}


			//得到实际使用的Charset
			String strCharset=mapHeader.get("Charset");
			if (null==strCharset || strCharset.isEmpty()){
				strCharset=mapHeader.get("charset");
				if (null==strCharset || strCharset.isEmpty()){
					strCharset="UTF-8";
				}
			}


			// 建立实际的连接
			connHttps.connect();


			brIn = new BufferedReader(
					new InputStreamReader(connHttps.getInputStream(),strCharset));
			String strLine;

			StringBuilder sbResult=new StringBuilder();

			while ((strLine = brIn.readLine()) != null) {
				sbResult.append("\n").append(strLine); //readLine不包含换行符
			}
			connHttps.disconnect();
			strResult=sbResult.toString();


		} catch (Exception e) {
			//System.out.println("发送GET请求出现异常!" + e);
			throw new IOException("WebUtil发送GET请求异常",e);
			//e.printStackTrace();
			//throw e;
		} finally {// 使用finally块来关闭输入流
			try {
				if (brIn != null) {
					brIn.close();
					brIn=null;
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return strResult;
	}

	public static String sendGet(String strUrl, Map<String,String> mapParam, Map<String,String> mapHeader) throws Exception {
		StringBuilder sbParam=new StringBuilder("");
		int i1=0;
		Set<String> setKey=mapParam.keySet();
		for (String strKey : setKey ) {

		    sbParam.append(strKey).append("=").append(mapParam.get(strKey));
		    if (i1<setKey.size()-1){
		    	sbParam.append("&");
		    }
		    i1++;
		}
		return sendGet(strUrl, sbParam.toString(), mapHeader);
	}

	public static String sendPost(String strUrl, Map<String, String> mapParam, Map<String, String> mapHeader)
			throws Exception {
		return sendPost(strUrl, mapParam, mapHeader, 5000);
	}

	public static String sendPost(String strUrl, String strParam, Map<String, String> mapHeader) throws Exception {
		return sendPost(strUrl, strParam, mapHeader, 5000);
	}

	/**
	 * 新版的 POST请求
	 *
	 * @param url
	 *            发送请求的URL
	 * @param param
	 *            请求参数,请求参数应该是name1=value1&name2=value2的形式。
	 * @return URL所代表远程资源的响应
	 */
	public static String sendPost(String strUrl, String strParam , Map<String,String> mapHeader, Integer timeout) throws Exception {
		PrintWriter brOut = null;
		BufferedReader brIn = null;
		String strResult = "";
		try {
			System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
			URL realUrl = new URL(strUrl);
			// 打开和URL之间的连接
			//URLConnection conn = realUrl.openConnection();
			HttpURLConnection connHttps = (HttpURLConnection)realUrl.openConnection();
			if (null != timeout) {
				connHttps.setConnectTimeout(timeout);
				connHttps.setReadTimeout(timeout);
			}
			connHttps.setRequestMethod("POST");
			connHttps.setUseCaches(false);

			// 设置默认通用的请求属性
			connHttps.setRequestProperty("Accept", "*/*");
			connHttps.setRequestProperty("Charset", "UTF-8");
			connHttps.setRequestProperty("Connection", "Keep-Alive");
			connHttps.setRequestProperty("User-Agent", "Server(Java) WebUtil");

			if (null!=mapHeader && !mapHeader.isEmpty()){
				for(String strKey:mapHeader.keySet()){
					String strValue=mapHeader.get(strKey);

					char cFirst = strKey.charAt(0);
					if(cFirst < 65 || cFirst > 90){ //不是大写开头
						//System.out.println( new StringBuilder("A http header parameter '").append(strKey).append("' isn't start with capital letter.."));
					}
					if (null!=strValue && !strValue.isEmpty()){
						connHttps.setRequestProperty(strKey, strValue);

					}
				}
			}

			/*
			if (null!=contentType && !contentType.isEmpty()){
				conn.setRequestProperty("Content-Type",contentType);
			}*/

			// 发送POST请求必须设置如下两行
			connHttps.setDoOutput(true);
			connHttps.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			brOut = new PrintWriter(connHttps.getOutputStream());


			//得到实际使用的Charset
			String strCharset=mapHeader.get("Charset");
			if (null==strCharset || strCharset.isEmpty()){
				strCharset=mapHeader.get("charset");
				if (null==strCharset || strCharset.isEmpty()){
					strCharset="UTF-8";
				}
			}

			// 发送请求参数
			brOut.print(new String(strParam.getBytes(strCharset)));
			// flush 输出流的缓冲
			brOut.flush();

			//System.out.println(connHttps.getResponseCode());

			// 定义BufferedReader输入流来读取URL的响应
			brIn = new BufferedReader(
					new InputStreamReader(connHttps.getInputStream(),strCharset));
			String strLine;

			StringBuilder sbResult=new StringBuilder();

			while ((strLine = brIn.readLine()) != null) {
				sbResult.append(strLine).append("\n"); //readLine不包含换行符
			}
			if (null != sbResult && sbResult.length() > 0) {
				sbResult.setLength(sbResult.length() - 1);
			}
			connHttps.disconnect();
			strResult=sbResult.toString();


		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally {// 使用finally块来关闭输出流、输入流.

			try {
				if (brOut != null) {
					brOut.close();
					brOut=null;
				}
				if (brIn != null) {
					brIn.close();
					brIn=null;
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return strResult;
	}

	public static String sendPost(String url, Map<String,String> mapParam ,Map<String,String> mapHeader, Integer timeout) throws Exception {

		StringBuilder sbParam=new StringBuilder("");
		int i1=0;
		Set<String> setKey=mapParam.keySet();
		for (String strKey : setKey ) {

		    sbParam.append(strKey).append("=").append(mapParam.get(strKey));
		    if (i1<setKey.size()-1){
		    	sbParam.append("&");
		    }
		    i1++;
		}

		return sendPost(url, sbParam.toString(), mapHeader, timeout);
	}





	/**
	 * 安全的 Get 请求
	 *
	 *
	 *
	 **/
	public static String sendSecureGet(String strURL, String strParam, Map<String,String> mapHeader
			,TrustManager tmIn) throws Exception {

		BufferedReader brIn=null;
		String strResult=null;

		try {

			// 创建SSLContext对象,并使用指定的信任管理器初始化
			TrustManager[] arrayTM = { tmIn };
			SSLContext sslc = SSLContext.getInstance("SSL", "SunJSSE");
			sslc.init(null, arrayTM, new java.security.SecureRandom());
			// 从上述SSLContext对象中得到SSLSocketFactory对象
			SSLSocketFactory sslsf = sslc.getSocketFactory();

			URL urlHttps = new URL(strURL);
			HttpsURLConnection connHttps = (HttpsURLConnection)(urlHttps.openConnection());
			connHttps.setSSLSocketFactory(sslsf);

			//设置请求方式(GET/POST)
			connHttps.setRequestMethod("GET");

			connHttps.setUseCaches(false);

			//设置通用的请求属性
			connHttps.setRequestProperty("Accept", "*/*");
			connHttps.setRequestProperty("Charset", "UTF-8");
			connHttps.setRequestProperty("Connection", "Keep-Alive");
			connHttps.setRequestProperty("User-Agent", "Server(Java) WebUtil");

			if (null!=mapHeader && !mapHeader.isEmpty()){
				for(String strKey:mapHeader.keySet()){
					String strValue=mapHeader.get(strKey);

					char cFirst = strKey.charAt(0);
					if(cFirst < 65 || cFirst > 90){ //不是大写开头
						//System.out.println( new StringBuilder("A http header parameter '").append(strKey).append("' isn't start with capital letter.."));
					}
					if (null!=strValue && !strValue.isEmpty()){
						connHttps.setRequestProperty(strKey, strValue);

					}
				}
			}


			//得到实际使用的Charset
			String strCharset=mapHeader.get("Charset");
			if (null==strCharset || strCharset.isEmpty()){
				strCharset=mapHeader.get("charset");
				if (null==strCharset || strCharset.isEmpty()){
					strCharset="UTF-8";
				}
			}


			// 建立实际的连接
			connHttps.connect();


			// 定义BufferedReader输入流来读取URL的响应
			brIn = new BufferedReader(
					new InputStreamReader(connHttps.getInputStream(),strCharset));
			String strLine;

			StringBuilder sbResult=new StringBuilder();

			while ((strLine = brIn.readLine()) != null) {
				sbResult.append("\n").append(strLine); //readLine不包含换行符
			}
			connHttps.disconnect();
			strResult=sbResult.toString();

		} catch (Exception e) {
			throw new IOException("WebUtil发送安全的GET请求异常",e);
		} finally {// 使用finally块来关闭输出流、输入流.
			try {
				if (brIn != null) {
					brIn.close();
					brIn=null;
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return strResult;
	}

	public static String sendSecureGet(String strUrl, Map<String,String> mapParam, Map<String,String> mapHeader,TrustManager tmIn) throws Exception {
		StringBuilder sbParam=new StringBuilder("");
		int i1=0;
		Set<String> setKey=mapParam.keySet();
		for (String strKey : setKey ) {

		    sbParam.append(strKey).append("=").append(mapParam.get(strKey));
		    if (i1<setKey.size()-1){
		    	sbParam.append("&");
		    }
		    i1++;
		}
		return sendSecureGet(strUrl, sbParam.toString(), mapHeader,tmIn);
	}


	/**
	 * 安全的 Post 请求
	 *
	 *
	 *
	 **/
	public static String sendSecurePost(String strURL, String strParam, Map<String,String> mapHeader
			,TrustManager tmIn) throws Exception {

		BufferedReader brIn=null;
		PrintWriter brOut=null;
		String strResult=null;

		try {

			// 创建SSLContext对象,并使用指定的信任管理器初始化
			TrustManager[] arrayTM = { tmIn };
			SSLContext sslc = SSLContext.getInstance("SSL", "SunJSSE");
			sslc.init(null, arrayTM, new java.security.SecureRandom());
			// 从上述SSLContext对象中得到SSLSocketFactory对象
			SSLSocketFactory sslsf = sslc.getSocketFactory();

			URL urlHttps = new URL(strURL);
			HttpsURLConnection connHttps = (HttpsURLConnection)(urlHttps.openConnection());
			connHttps.setSSLSocketFactory(sslsf);

			//设置请求方式(GET/POST)
			connHttps.setRequestMethod("POST");

			//设置POST所需配置
			connHttps.setDoOutput(true);
			connHttps.setDoInput(true);

			connHttps.setUseCaches(false);

			//设置通用的请求属性
			connHttps.setRequestProperty("Accept", "*/*");
			connHttps.setRequestProperty("Charset", "UTF-8");
			connHttps.setRequestProperty("Connection", "Keep-Alive");
			connHttps.setRequestProperty("User-Agent", "Server(Java) WebUtil");

			if (null!=mapHeader && !mapHeader.isEmpty()){
				for(String strKey:mapHeader.keySet()){
					String strValue=mapHeader.get(strKey);

					char cFirst = strKey.charAt(0);
					if(cFirst < 65 || cFirst > 90){ //不是大写开头
						//System.out.println( new StringBuilder("A http header parameter '").append(strKey).append("' isn't start with capital letter.."));
					}
					if (null!=strValue && !strValue.isEmpty()){
						connHttps.setRequestProperty(strKey, strValue);

					}
				}
			}

			//得到实际使用的Charset
			String strCharset=mapHeader.get("Charset");
			if (null==strCharset || strCharset.isEmpty()){
				strCharset=mapHeader.get("charset");
				if (null==strCharset || strCharset.isEmpty()){
					strCharset="UTF-8";
				}
			}

			// 获取URLConnection对象对应的输出流
			brOut = new PrintWriter(connHttps.getOutputStream());

			// 发送请求参数
			if (null!=strParam){
				brOut.print(new String(strParam.getBytes(strCharset)));
				brOut.flush();
			}


			// 定义BufferedReader输入流来读取URL的响应
			brIn = new BufferedReader(new InputStreamReader(connHttps.getInputStream(),strCharset));
			String strLine;

			StringBuilder sbResult=new StringBuilder();

			while ((strLine = brIn.readLine()) != null) {
				sbResult.append("\n").append(strLine); //readLine不包含换行符
			}
			connHttps.disconnect();
			strResult=sbResult.toString();

		} catch (Exception e) {
			throw new IOException("WebUtil发送安全的POST请求异常",e);
		} finally { // 使用finally块来关闭输出流、输入流.
			try {
				if (brIn != null) {
					brIn.close();
					brIn=null;
				}
				if (brOut != null) {
					brOut.close();
					brOut=null;
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return strResult;
	}

	public static String sendSecurePost(String url, Map<String,String> mapParam ,Map<String,String> mapHeader,TrustManager tmIn) throws Exception {

		StringBuilder sbParam=new StringBuilder("");
		int i1=0;
		Set<String> setKey=mapParam.keySet();
		for (String strKey : setKey ) {

		    sbParam.append(strKey).append("=").append(mapParam.get(strKey));
		    if (i1<setKey.size()-1){
		    	sbParam.append("&");
		    }
		    i1++;
		}

		return sendSecurePost(url, sbParam.toString(), mapHeader,tmIn);
	}


	/**
	 * 获取请求的PostBody
	 *
	 *
	 *
	 **/
	public static String getPostBody(HttpServletRequest request)
			throws IOException {
		// 解析结果存储在HashMap
		//Map<String, Object> map = new HashMap<String, Object>();
		String str = "";
		BufferedReader reader = null;
		try {
			StringBuilder buffer = new StringBuilder();
			reader = new BufferedReader(new InputStreamReader(
					request.getInputStream(), "UTF-8"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
			str = buffer.toString();
			//map = JSONUtil.json2map(str);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}

		return str;
	}

	/**
     * 向指定 URL 发送POST方法 发送PostBody
     *
     * @param url
     *
     * @param param
     *
     * @return
     */
	/*
	@Deprecated
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "* /*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (ConnectException ce) {
        	System.out.println("发送 POST 请求出现异常(Connection timed out!)");
			//logger.info(url+" (post failed)");
        	ce.printStackTrace();
        } catch (Exception e) {
        	System.out.println("发送 POST 请求出现异常!"+e);
            e.printStackTrace();
        }finally{//使用finally块来关闭输出流、输入流
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }*/

	/**
	 * 处理用户上传
	 */
	public static String handleUpload(HttpServletRequest request, HttpServletResponse response, String strFilePath,String strFileName)
		throws Exception  {
		//String strFileName = request.getParameter("fileName");
		//strFileName = new String(strFileName.getBytes("ISO-8859-1"),"UTF-8");
		//String overFlg = request.getParameter("over"); // 0:go on;1:over
		//System.out.println("get: " + fileName);
		byte[] buf = new byte[4096];
		if (strFilePath.charAt(strFilePath.length()-1)!='/'){
			strFilePath+="/";
		}
		strFileName=strFilePath+strFileName;
		File file = new File(strFileName);
		InputStream is = null;
		BufferedOutputStream fileOut
			= new BufferedOutputStream(new FileOutputStream(file, true));
        try {
        	is = request.getInputStream();

            while (true) {
            	int bytesIn = is.read(buf, 0, 4096);
                //System.out.println(bytesIn);
                if (bytesIn == -1) {
                    break;
                } else {
                    fileOut.write(buf, 0, bytesIn);
                }
            }

            fileOut.flush();
            fileOut.close();
            //System.out.println(file.getAbsolutePath());
        } catch (IOException e) {
            throw e;
        }
        return strFileName;
    }







}