使用httpclient发送get/post请求

来源:互联网 发布:方块导航网源码 编辑:程序博客网 时间:2024/06/05 07:24

当我们需要调用http接口时,可以使用httpclient以get/post方式发送请求,获取返回的json数据

maven依赖如下

<dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>httpcore</artifactId>            <version>4.4.4</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>httpclient</artifactId>            <version>4.5.2</version>        </dependency>
package com.yingjun.ssm;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;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.HttpClients;import org.apache.http.util.EntityUtils;/** * Created by cjq on 2017/8/23. */public class HttpUtil {    /**     * 以get方式发送请求     * @param url     * @return     */    public static JSONObject doGetStr(String url){        HttpClient httpClient= HttpClients.custom().build();        HttpGet httpGet=new HttpGet(url);        JSONObject jsonpObject=null;        try {            HttpResponse response = httpClient.execute(httpGet);            HttpEntity entity=response.getEntity();            if(entity!=null){                String result= EntityUtils.toString(entity,"UTF-8");                jsonpObject= JSON.parseObject(result);            }        }catch (Exception e){        }        return  jsonpObject;    }    /**     * post请求     * @param url     * @param outStr     * @return  JSONObject /String     */    public static JSONObject doPostStr(String url,String outStr){        HttpClient httpClient=HttpClients.custom().build();        HttpPost httpPost=new HttpPost(url);        JSONObject jsonObject=null;        String result="";        try {            httpPost.setEntity(new StringEntity(outStr, "UTF-8"));            HttpResponse respons=httpClient.execute(httpPost);            result=EntityUtils.toString(respons.getEntity(),"UTF-8");            jsonObject= JSON.parseObject(result);        }catch (Exception e){            e.printStackTrace();        }        return  jsonObject;    }    public String doPostMethod(String url,String outStr){        HttpClient httpClient=HttpClients.custom().build();        String result = "";        HttpPost httpPost = null;        HttpResponse httpResponse = null;        HttpEntity entity = null;        httpPost = new HttpPost(url);        try {            httpPost.setEntity(new StringEntity(outStr, "UTF-8"));            httpResponse = httpClient.execute(httpPost);            entity = httpResponse.getEntity();            if( entity != null ){                result = EntityUtils.toString(entity,"UTF-8");            }        }catch (Exception e){            e.printStackTrace();        }        return result;    }    public static void main(String args[])  {           HttpUtil httpUtil=new HttpUtil();        JSONObject jsonObject=  httpUtil.doGetStr("http://gc.ditu.aliyun.com/geocoding?a=苏州市");        System.out.println(jsonObject.toJSONString());    }}

结果如下

{"address":"","cityName":"","alevel":4,"level":2,"lon":120.58531,"lat":31.29888}