HttpClientUtil工具类


import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.*;


public class HttpClientUtil {
    private static HttpClient client = null;
    private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);


    private HttpClientUtil(){}


    static{
        if(null == client) {
            client = HttpClientFactory.createHttpClient();
        }
    }


    public static String doGetRequest(String urlstr) {
        String response = "";


        HttpGet httpGet = new HttpGet(urlstr);
        try {
            HttpResponse httpResponse = client.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
        } catch (Exception e) {
            logger.error("错误信息" + e.getMessage());
            e.printStackTrace();
        } finally {
            httpGet.releaseConnection();


        }
        return response.trim();
    }


    /**
     *
     * @param _InputStream
     * @param Charset
     * @return
     */
    public String GetResponseString(InputStream _InputStream, String Charset) {
        String response = "";
        try {
            if (_InputStream != null) {
                StringBuffer buffer = new StringBuffer();
                InputStreamReader isr = new InputStreamReader(_InputStream, Charset);
                Reader in = new BufferedReader(isr);
                int ch;
                while ((ch = in.read()) > -1) {
                    buffer.append((char) ch);
                }
                response = buffer.toString();
                buffer = null;
            }
        } catch (Exception e) {
            logger.error("错误信息" + e.getMessage());
            response = response + e.getMessage();
            e.printStackTrace();
        }
        return response;
    }


    public static void main(String[] args) {
        String url = "http://xxx?spid=3060&sppassword=hbkj3060&das=8618611178949&command=MULTI_MT_REQUEST&sm=a1beccd4b1a6a1bf20cda8b5c0bdd3c8ebcdeab3c9a3a1&dc=15";
        url = "http://xxx/sdkproxy/regist.action?cdkey=2SDK-EMY-6688-AAAAA&password=123456";
//        Map map = XMLConverUtil.convertToMap(doGetRequest(url));
//        System.out.println(map);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值