httpclient

post

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


 // http请求
    public static Result Post(String path, Map<String, String> param) {
        String url = "http://192.168.1.115:9191" + path;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Host", "****");
        headerMap.put("Accept-Encoding", "gzip");
        headerMap.put("charset", "utf-8");
        List<NameValuePair> pList = new ArrayList<>();
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            pList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        for (Map.Entry<String, String> entry : param.entrySet()) {
            pList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        try {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pList, "UTF-8");
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();
            String content = "";
            if (resEntity != null) {
                content = EntityUtils.toString(resEntity, "UTF-8");
            }
            Result res = new Gson().fromJson(content, Result.class);
            return res;
        } catch (Exception e) {
            String msg = "请求数说读者的GO服务失败, url: " + url;
            LogDefine.LOG.error(msg, e);
            return new Result<String>(null, false, msg, null);
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值