赞
踩
前言:
正文:
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; // HTTP GET request public static String SendGet(String url) { HttpClient client = new HttpClient(); GetMethod method = new GetMethod(url); try { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { logger.error("获取SendGet失败:" + method.getStatusLine()); } return method.getResponseBodyAsString(); } catch (HttpException e) { logger.error("获取SendGet失败: Fatal protocol violation", e); } catch (IOException e) { logger.error("获取SendGet失败: transport error", e); } finally { method.releaseConnection(); } return ""; }
// HTTP POST public static String SendPOST(String url) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); try { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { logger.error("获取SendPOST失败:" + method.getStatusLine()); } return method.getResponseBodyAsString(); } catch (HttpException e) { logger.error("获取SendPOST失败: Fatal protocol violation", e); } catch (IOException e) { logger.error("获取SendPOST失败: transport error", e); } finally { method.releaseConnection(); } return ""; }
(
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。