<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;apache
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;app
public class HttpClientUtils {
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
public static String getResFromGetRequestUrl(String url) {
String res = HttpClientUtils.doGet(url, null, "UTF-8", false);
return res;
}maven
public static String doGet(String url, String queryString, String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
try {
if (StringUtils.isNotBlank(queryString))
// 對get請求參數作了http請求默認編碼,好像沒有任何問題,漢字編碼後,就成爲%式樣的字符串
method.setQueryString(URIUtil.encodeQuery(queryString));
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(method.getResponseBodyAsStream(), charset));
String line;
while ((line = reader.readLine()) != null) {
if (pretty)
response.append(line).append(System.getProperty("line.separator"));
else
response.append(line);
}
reader.close();
}
} catch (URIException e) {
logger.error("執行HTTP Get請求時,編碼查詢字符串「" + queryString + "」發生異常!", e);
} catch (IOException e) {
logger.error("執行HTTP Get請求" + url + "時,發生異常!", e);
} finally {
method.releaseConnection();
}
return response.toString();
}
/**
* 執行一個HTTP POST請求,返回請求響應的HTML
*
* @param url 請求的URL地址
* @param params 請求的查詢參數,能夠爲null
* @param charset 字符集
* @param pretty 是否美化
* @return 返回請求響應的HTML
*/
public static String doPost(String url, Map<String, String> params, String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
//設置參數的字符集
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,charset);
//設置Http Post數據
if (params != null) {
//HttpMethodParams p = new HttpMethodParams();
for (Map.Entry<String, String> entry : params.entrySet()) {
//p.setParameter(entry.getKey(), entry.getValue());
method.setParameter(entry.getKey(), entry.getValue());
}
//method.setParams(p);
}
try {
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
String line;
while ((line = reader.readLine()) != null) {
if (pretty)
response.append(line).append(System.getProperty("line.separator"));
else
response.append(line);
}
reader.close();
}
} catch (IOException e) {
logger.error("執行HTTP Post請求" + url + "時,發生異常!", e);
} finally {
method.releaseConnection();
}
return response.toString();
}
}
工具