`
thrillerzw
  • 浏览: 138784 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

httpclient使用

    博客分类:
  • java
 
阅读更多
/**
	 * 
	 * @Description: httpclient post请求 
	 * @author thrillerzw
	 */
	public static String httpclientPostRequest(String url, Map<String, String> paramsMap) throws HttpException, IOException {
		HttpClient client = new HttpClient();
		//使用POST方法
		PostMethod method = new PostMethod(url);

		//添加参数
		if (paramsMap != null) {
			Iterator<Map.Entry<String, String>> iterator = paramsMap.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry<String, String> entry = iterator.next();
				String key = entry.getKey();
				String value = entry.getValue();
				((PostMethod) method).addParameter(key, value);
			}
		}
		HttpMethodParams param = method.getParams();
		param.setContentCharset("UTF-8");
		client.executeMethod(method);
		int statusCode = method.getStatusCode();
		if (statusCode != 200) {
			return "";
		}
		InputStream stream = method.getResponseBodyAsStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
		StringBuffer buf = new StringBuffer();
		String line;
		while (null != (line = br.readLine())) {
			buf.append(line).append("\n");
		}
		//释放连接
		method.releaseConnection();
		// System.out.println(buf.toString());
		return buf.toString();
	}
	
	//url检测连接超时时间
	private static final int URL_CHECK_CONN_TIME=20000;
	//url检测读取超时时间
	private static final int URL_CHECK_SO_TIME=20000;
	/**
	 * 
	 * @Description: get方式判断是否正确url
	 * @author thrillerzw
	 */
	public static boolean isRightUrl(String url) {
		if (StringUtils.isEmpty(url)) {
			return false;
		}
		String urlLowercase=url.toLowerCase();
		if (!urlLowercase.startsWith("http") && !urlLowercase.startsWith("https")) {
			return false;
		}
		//白名单
		if (isWhiteList(url)) {
			return true;
		}
		//替换特殊字符
		url = buildUrl(url);

		int statusCode=0;
		statusCode = getUrlRespongseCode(url,URL_CHECK_CONN_TIME,URL_CHECK_SO_TIME);
		if (statusCode == 301) {
			String newUrl = get301NewUrl(url,URL_CHECK_CONN_TIME,URL_CHECK_SO_TIME);
			statusCode=getUrlRespongseCode(newUrl,URL_CHECK_CONN_TIME,URL_CHECK_SO_TIME);
		}
		if (statusCode == 200) {
			return true;
		}
		return false;
	}
	/**
	 * 
	 * @Description: get方式获取url响应码,如果异常,返回0
	 * @author thrillerzw
	 */
	public static GetMethod getGetMtethod(String url,int contimeout,int sotimeout){
		HttpClient httpClient = new HttpClient();
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(contimeout);
		httpClient.getHttpConnectionManager().getParams().setSoTimeout(sotimeout);
		GetMethod getMethod = null;
		try {
			getMethod = new GetMethod(url);
			//禁掉自动处理重定向
			getMethod.setFollowRedirects(false);
			httpClient.executeMethod(getMethod);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return getMethod;
	}

	/**
	 * 
	 * @Description: get方式获取url响应码,如果异常,返回0
	 * @author thrillerzw
	 */
	public static int getUrlRespongseCode(String url,int contimeout,int sotimeout){
		int statusCode = 0;
		GetMethod getMethod=null;
		try {
			getMethod=getGetMtethod(url,contimeout,sotimeout);
			statusCode=getMethod.getStatusCode();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//释放连接
			if(getMethod!=null){
				getMethod.releaseConnection();
			}
		}
		return statusCode;
	}
	
	
	/**
	 * 
	 * @Description: get方式获取301重定向到的地址url
	 *
	 * @author thrillerzw
	 */
	public static String get301NewUrl(String url,int contimeout,int sotimeout){
		
		GetMethod getMethod = null;
		String newUrl="";
		try {
			getMethod=getGetMtethod(url,contimeout,sotimeout);
			Header header = getMethod.getResponseHeader("Location");
			newUrl = header.getValue();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//释放连接
			if(getMethod!=null){
				getMethod.releaseConnection();
			}
		}
		return newUrl;
	}

	//替换url特殊字符
	private static String buildUrl(String url) {
		if (StringUtils.isEmpty(url)) {
			return "";
		}
		//替换url
		url = url.replaceAll(" ", "%20");
		StringBuffer urlSb = new StringBuffer();
		//处理中文转码
		for (int i = 0; i < url.length(); i++) {
			char ch = url.charAt(i);
			Character.UnicodeBlock ub = Character.UnicodeBlock.of(ch);
			if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
					|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) {
				//目前已知的中文字符UTF-8集合
				try {
					String str = URLEncoder.encode(String.valueOf(ch), "utf-8");
					urlSb.append(str);
				} catch (UnsupportedEncodingException e) {
					urlSb.append(ch);
					e.printStackTrace();
				}
			} else {
				urlSb.append(ch);
			}
		}
		return urlSb.toString();
	}

	//url是否包含白名单域名
	private static boolean isWhiteList(String url) {
		String whitelist = Constants.URL_DOMAIN_WHITELIST;
		if (StringUtils.isEmpty(url) || StringUtils.isEmpty(whitelist)) {
			return false;
		}

		String[] whiteUrlDomainArr = whitelist.split(",");
		for (String whiteUrlDomain : whiteUrlDomainArr) {
			if (url.toLowerCase().contains(whiteUrlDomain.toLowerCase())) {
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) throws HttpException, IOException, URISyntaxException {
		String url="http://www.baidu.com";
		boolean res = isRightUrl(url);
		System.out.println("res="+res);
	}

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics