当前位置: 移动技术网 > IT编程>开发语言>Java > java实现http请求工具类示例

java实现http请求工具类示例

2019年07月22日  | 移动技术网IT编程  | 我要评论

通过http rest请求返回数据

复制代码 代码如下:

import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.apache.http.namevaluepair;
import org.apache.http.client.clientprotocolexception;
import org.apache.http.client.httpclient;
import org.apache.http.client.responsehandler;
import org.apache.http.client.entity.urlencodedformentity;
import org.apache.http.client.methods.httpget;
import org.apache.http.client.methods.httppost;
import org.apache.http.impl.client.basicresponsehandler;
import org.apache.http.impl.client.defaulthttpclient;
import org.apache.http.impl.conn.tsccm.threadsafeclientconnmanager;

import java.io.ioexception;
import java.text.messageformat;
import java.util.list;
import java.util.concurrent.timeunit;

/**
 * 分装一个http请求的工具类
 *
 * @author 顾炜【guwei】 on 14-4-22.下午3:17
 */
public class httpclientutils {

    private static final log log = logfactory.getlog(httpclientutils.class);
    /**
     * 初始化httpclient
     */
    private static httpclient httpclient = null;

    /**
     * 生产httpclient实例
     * 公开,静态的工厂方法,需要使用时才去创建该单体
     *
     * @return
     */
    public static httpclient gethttpclient() {
        if (httpclient == null) {
            httpclient = new defaulthttpclient(new threadsafeclientconnmanager());
        }
        return httpclient;
    }

    /**
     * post方式调用
     *
     * @param url
     * @param params 参数为namevaluepair键值对对象
     * @return 响应字符串
     * @throws java.io.unsupportedencodingexception
     */
    public static string executebypost(string url, list<namevaluepair> params) {
        httpclient httpclient = gethttpclient();

        httppost post = new httppost(url);


        responsehandler<string> responsehandler = new basicresponsehandler();
        string responsejson = null;
        try {
            if (params != null) {
                post.setentity(new urlencodedformentity(params));
            }
            responsejson = httpclient.execute(post, responsehandler);
            log.info("httpclient post请求结果:" + responsejson);
        } catch (clientprotocolexception e) {
            e.printstacktrace();
            log.info("httpclient post请求异常:" + e.getmessage());
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            httpclient.getconnectionmanager().closeexpiredconnections();
            httpclient.getconnectionmanager().closeidleconnections(30, timeunit.seconds);
        }
        return responsejson;
    }

    /**
     * get方式请求
     *
     * @param url    带参数占位符的url,例:http://****/user/user/center.aspx?_action=getsimpleuserinfo&codes={0}&email={1}
     * @param params 参数值数组,需要与url中占位符顺序对应
     * @return 响应字符串
     * @throws java.io.unsupportedencodingexception
     */
    public static string executebyget(string url, object[] params) {
        httpclient httpclient = gethttpclient();

        string messages = messageformat.format(url, params);

        httpget get = new httpget(messages);

        responsehandler<string> responsehandler = new basicresponsehandler();
        string responsejson = null;
        try {
            responsejson = httpclient.execute(get, responsehandler);
            log.info("httpclient get请求结果:" + responsejson);
        } catch (clientprotocolexception e) {
            e.printstacktrace();
            log.info("httpclient get请求异常:" + e.getmessage());
        } catch (ioexception e) {
            e.printstacktrace();
            log.info("httpclient get请求异常:" + e.getmessage());
        } finally {
            httpclient.getconnectionmanager().closeexpiredconnections();
            httpclient.getconnectionmanager().closeidleconnections(30, timeunit.seconds);
        }
        return responsejson;
    }

    /**
     * @param url
     * @return
     */
    public static string executebyget(string url) {
        httpclient httpclient = gethttpclient();

        httpget get = new httpget(url);

        responsehandler<string> responsehandler = new basicresponsehandler();
        string responsejson = null;
        try {
            responsejson = httpclient.execute(get, responsehandler);
            log.info("httpclient get请求结果:" + responsejson);
        } catch (clientprotocolexception e) {
            e.printstacktrace();
            log.info("httpclient get请求异常:" + e.getmessage());
        } catch (ioexception e) {
            e.printstacktrace();
            log.info("httpclient get请求异常:" + e.getmessage());
        } finally {
            httpclient.getconnectionmanager().closeexpiredconnections();
            httpclient.getconnectionmanager().closeidleconnections(30, timeunit.seconds);
        }
        return responsejson;
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网