Http请求工具类

package com.*.*.*.util;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;

public class HttpUtils {
/*    @Autowired
    private  SysConfigMapper sysConfigMapper;*/
    private  static Logger logger = Logger.getLogger(HttpUtils.class);
    static String web_url ="http://192.168.100.53:8080/*/*/" ;        
    static{
        //初始化url参数
        new HttpUtils().poolWebServiceUrl();
    }

    
    void poolWebServiceUrl() {
/*        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        SysConfig jpushConf = sysConfigMapper.selectByConfigkey("DEAL_ORGAN_PATH");
        web_url=jpushConf.getConfigvalue();*/

    }

    /**
     * @param url 请求地址
     * @param jsonObject 请求的json数据
     * @return
     */
    public static String jsonPost(String url, JSONObject jsonObject) {
        url=web_url+url+".html";
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        String result = null;
        try {
            post.addHeader("Content-type","application/json; charset=utf-8");
            post.setHeader("Accept", "application/json");
            post.setEntity(new StringEntity(jsonObject.toString(), Charset.forName("UTF-8")));
            HttpResponse res = httpclient.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(res.getEntity());// 返回json格式:
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
    
    
    /**
     * @param url 请求地址
     * @param jsonObject 请求的json数据
     * @return
     */
    public static String jsonPost(String url, String args) {
        url=web_url+url+".html";
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        String result = null;
        try {
            post.addHeader("Content-type","application/json; charset=utf-8");
            post.setHeader("Accept", "application/json");
            post.setEntity(new StringEntity(args, Charset.forName("UTF-8")));
            HttpResponse res = httpclient.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(res.getEntity());// 返回json格式:
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
    /**
     * get请求
     * @return
     */
    public static String doGet(String url) {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        try {
            //发送get请求
            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);

            /**请求发送成功,并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity());

                return strResult;
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值