/** * * @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); }
相关推荐
httpclient使用教程
使用httpClient进行代理
httpclient使用详解共8页.pdf.zip
Http协议使用封装jar包(commons-codec-1.3.jar、commons-httpclient-3.1.jar、commons-logging-1.1.jar) 简单使用方法: public static void main(String[] args) { // String str1 = "...
URLConnection和HttpClient使用入门
httpClient 教程 使用事例 单向认证
httpclient是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,本文档提供使用httpclient的使用方法
HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 适用人群:JavaWeb开发者 使用场景:在Java后端程序中,模拟浏览器端进行Web请求。 其它:...
Httpclient,一个我们平时开发时经常用到的组件
在.Net Core应用开发中,调用第三方接口也是常有的事情,HttpClient使用人数、使用频率算是最高的一种了,在.Net Core中,HttpClient的使用方式随着版本的升级也发生了一些变化,本次就讲解一下Asp.Net Core2.1前后...
完整的上传实例包括图片和参数配置,可以通过该样例进行扩展使用
NULL 博文链接:https://zhcheng.iteye.com/blog/1292350