在之前的开发过程中需要用到httpclient的请求方法,虽然使用httpclient类中的相关方法可以实现基本请求的方法,可是还是觉得每次都要写一段固定的代码,方法中稍微有参数的改变就要重新实现一遍,限制了代码的扩展性。于是自己动手实现了一个模仿httpclient的工具类,此类不是很麻烦。具体代码如下:
/**
* 根据指定的 http 客户端对象和 请求方法,发送 http 请求并返回结果。
* <p />
* 如果请求失败,则返回 {@code null}。
*
* @param <T> {@code HttpMethod} 的实现。
* @param client http 请求客户端。
* @param method http 执行方法。
* @param String charset 编码。
* @return http 请求的结果。
*/
public static <T extends HttpMethod> String sendRequest(HttpClient client, T method, String charset) {
if (StringUtils.isBlank(charset)) {
charset = CharEncoding.UTF_8;
}
int status = 0;
try {
status = client.executeMethod(method);
LOGGER.debug(String.format("http request url: %s, status: %s", method.getPath(), status));
return IOUtils.toString(method.getResponseBodyAsStream(), charset);
} catch (Exception ex) {
LOGGER.error(String.format("http request url: %s, status: %s, error: %s", method.getPath(),
status, ex.getMessage()));
return null;
}
}/**
* 发送指定 {@code url} 和参数的 http 请求,如果 {@code post == true},则发送 post 请求。
* <p />
* 请求失败,或发生错误时,返回 {@code null}。
*
* @param url 要发送请求的 {@code URL}。
* @param params {@code url} 的参数。
* @param charset 请求响应的编码。
* @param post 是否发送 {@code POST} 请求。
* @return 请求的结果。
*/
public static String sendRequest(String url, Map<String, String> params, String charset, boolean post) {
String noSchemeUrl = null;
if (StringUtils.startsWith(url, "http://")) {
noSchemeUrl = StringUtils.substring(url, 7);//将url去除协议部分以便后面获得host和path
} else if (StringUtils.startsWith(url, "https://")) {
noSchemeUrl = StringUtils.substring(url, 8);
}
int firstSlush = StringUtils.indexOf(noSchemeUrl, "/");//获得host和path
String host, path;
if (firstSlush != -1) {
host = noSchemeUrl.substring(0, firstSlush);
path = noSchemeUrl.substring(firstSlush);
} else {
host = noSchemeUrl;
path = "";
}
int colonIndex = StringUtils.indexOf(host, ":");//获得请求的端口号
int port = 0;
if (colonIndex != -1) {
host = StringUtils.substring(host, 0, colonIndex);
port = NumberUtils.toInt(StringUtils.substring(host, colonIndex), 80);
}
return sendRequest(host, port, path, params, charset, post);
}
/**
* 发送 http 请求。
*
* @param host 域名。
* @param port 端口号。
* @param path 路径。
* @param params 参数。
* @param charset 请求响应的编码。
* @param post 是否发送 {@code POST} 请求。
* @return 请求响应返回的结果。
*/
public static String sendRequest(String host, int port, String path, Map<String, String> params,
String charset, boolean post) {
HttpClient client = getHttpClient(host, port);
HttpMethod httpMethod = null;
if (post) {
httpMethod = new PostMethod(path);
} else {
httpMethod = new GetMethod(path);
}
if (params != null && params.size() > 0) {
List<NameValuePair> paramList = new LinkedList<NameValuePair>();
for (String key : params.keySet()) {
paramList.add(new NameValuePair(key, params.get(key)));//将请求参数设置到NameValuePair
}
httpMethod.setQueryString(paramList.toArray(new NameValuePair[paramList.size()]));
}
return sendRequest(client, httpMethod, charset);
}
/**
* 根据指定的 {@code host} 和 端口号 {@code port},构造一个新的 {@code HttpClient}。
*
* @param host 主机名(域名)。
* @param port 端口号。
* @return 返回指定 {@code host} 和 端口号 {@code port} 的 {@code HttpClient}。
*/
public static HttpClient getHttpClient(String host, int port) {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(host, port);
return client;
}
/**
* 发送 {@code GET} 请求。
*
* @param host 域名。
* @param port 端口号。
* @param path 路径。
* @param params 参数。
* @param charset 编码。
* @return 请求的结果。
*/
public static String sendGetRequest(String host, int port, String path, Map<String, String> params,
String charset) {
return sendRequest(host, port, path, params, charset, false);
}
/**
* 发送 {@code POST} 请求。
*
* @param host 域名。
* @param port 端口号。
* @param path 路径。
* @param params 参数。
* @param charset 编码。
* @return 请求的结果。
*/
public static String sendPostRequest(String host, int port, String path, Map<String, String> params,
String charset) {
return sendRequest(host, port, path, params, charset, true);
}
有了此封装类,就可以在代码中直接使用相应方法进行http请求了。
开发网页游戏过程记录6-自己实现apache httpclient

最新推荐文章于 2025-07-31 13:59:06 发布