您的位置:首页 > 理论基础 > 计算机网络

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  http