开发网页游戏过程记录6-自己实现apache httpclient

在之前的开发过程中需要用到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请求了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值