java http HttpClientUtil 工具类
2017-01-22 17:17
513 查看
public class HttpClientUtil { private static final Log logger = LogFactory.getLog(UploaderController.class); private static final String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.16) Gecko/20110319 BTRS96900 Firefox/3.6.16"; public static String getSourceByUrl(final String url) { HttpClient httpClient = new HttpClient(); String str = null; PostMethod getMethod = new PostMethod(url); getMethod.setRequestHeader("User-Agent",USER_AGENT); getMethod.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 0); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(0); try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { logger.error("Method failed: " + getMethod.getStatusLine()); } str = IOUtils.toString(getMethod.getResponseBodyAsStream(), "UTF-8"); } catch (UnknownHostException e) { logger.error("UnknownHostException: Please check your provided http address!"); } catch (HttpException e) { logger.error("HttpException: Please check your provided http address!"); } catch (ConnectException e) { logger.error("IOException: Please check your provided http address!"); } catch (IOException e) { logger.error("IOException: Please check your provided http address!"); } finally { getMethod.releaseConnection(); } return str; } public static String post(String url, NameValuePair[] params) { HttpClient httpClient = new HttpClient(); String str = null; PostMethod getMethod = new PostMethod(url); getMethod.setRequestHeader("User-Agent",USER_AGENT); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10000); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); if (params != null) { getMethod.addParameters(params); } try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { logger.error("Method failed: " + getMethod.getStatusLine() + ",url:[" + url + "]"); } str = getMethod.getResponseBodyAsString(); } catch (Exception e) { logger.error("HttpClientUtil post error.url:[" + url + "]", e); } finally { getMethod.releaseConnection(); } return str; } public static String get(String url) { HttpClient httpClient = new HttpClient(); String str = null; GetMethod getMethod = new GetMethod(url); getMethod.setRequestHeader("User-Agent",USER_AGENT); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10000); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000); httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { logger.error("Method failed: " + getMethod.getStatusLine() + ",url:[" + url + "]"); } str = getMethod.getResponseBodyAsString(); } catch (Exception e) { logger.error("HttpClientUtil get error.url:[" + url + "]", e); } finally { getMethod.releaseConnection(); } return str; } }
相关文章推荐
- Java工具类--通过HttpClient发送http请求
- java(优化22) org.apache.commons.httpclient.util.DateUtil取值问题
- J2EE工具类:WebHttpClient.java
- HttpClientProxyUtil HTTPClient请求工具类源码
- HttpClientUtil(HttpClient工具类)
- C# HttpClientUtil 工具类 表单请求提交 附件上传下载
- HttpClientUtil请求http地址的工具类
- J2EE工具类:ClientSocketUtil.java&ServerSocketConnection.java
- HttpClientUtil工具类
- java小程序检测web的并发数---HttpClient和util包的concurrent
- [工具类]HttpClientUtil简单工具
- HttpClientUtil工具类
- J2EE工具类:WebHttpClient.java
- HttpClientUtil工具类
- 常用工具类之HttpclientUtil
- 使用org.apache.commons.httpclient.util.DateUtil工具类解析时间减少一天
- java HttpClient 工具类
- java公众平台通用接口工具类HttpConnectUtil
- 【HttpClientUtil完整的工具类】
- 极光推送工具类 java httpClient