一个HttpClient调用的简单工具类

package com.gzisming.util.http;

import org.apache.http.HttpEntity;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;

import java.io.File;
import java.io.IOException;

/**
 * HTTP请求实现
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 * @since 2014/11/7
 * @version 1.0.0
 */
public class SimpleHttpClientUtils {

    public static final String TEXT_PLAIN = "text/plain";

    public static final String TEXT_HTML = "text/html";

    public static final String TEXT_XML = "text/xml";

    public static final String APPLICATION_XML = "application/xml";

    public static final String APPLICATION_JSON = "application/json";

    private static final StrResponseHandler respHandler = new StrResponseHandler();

    /**
     * GET请求
     *
     * @param url 请求地址
     * @return 响应内容
     * @throws java.io.IOException
     */
    public static String get(String url) throws IOException {
        String content = Request.Get(url).execute().handleResponse(respHandler);
        return content;
    }

    /**
     * POST请求
     *
     * @param url 请求地址
     * @param contentType 请求体类型[text, xml, json, html]
     * @param body 请求体
     * @return 响应内容
     * @throws java.io.IOException
     */
    public static String post(String url,
                              String contentType,
                              String body) throws IOException {
        String content = Request.Post(url)
                .bodyString(body, ContentType.create(contentType))
                .execute().handleResponse(respHandler);
        return content;
    }

    /**
     * 上传文件
     *
     * @param url 请求地址
     * @param file 上传文件
     * @return 响应内容
     * @throws java.io.IOException
     */
    public static String upload(String url, File file) throws IOException {
        HttpEntity media = MultipartEntityBuilder.create()
                .addPart("media", new FileBody(file)).build();
        String content = Request.Post(url).body(media)
                .execute().handleResponse(respHandler);
        return content;
    }

    /**
     * 下载文件
     *
     * @param url   请求地址
     * @param file 文件保存位置
     * @throws java.io.IOException
     */
    public static void download(String url, File file) throws IOException {
        Request.Get(url).execute().saveContent(file);
    }
}

   实现的ResponseHander子类,如下所示:


package com.gzisming.util.http;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * HTTP请求响应处理
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 * @since 2014/11/6
 * @version 1.0.0
 */
public class StrResponseHandler implements ResponseHandler<String> {

    @Override
    public String handleResponse(HttpResponse resp)
            throws ClientProtocolException, IOException {
        int status = resp.getStatusLine().getStatusCode();
        if (status >= 200 && status < 300) {
            HttpEntity entity = resp.getEntity();
            String body = (null!= entity) ? EntityUtils.toString(entity,"UTF-8") : ""; //处理中文编码
            return body;
        } else {
            throw new ClientProtocolException("请求失败,服务器响应代码: " + status);
        }
    }
}


转载于:https://my.oschina.net/hanhanxuexi/blog/366966

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值