使用httpclient发送get/post请求

当我们需要调用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}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值