发送Hppt请求

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
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.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.Closeable;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
       


        String url ="请求地址";
        String token ="";
        String body ="请求参数";
        HttpPost post = null;
        String responseMessange = "";
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            post = new HttpPost(url);
            // 构造消息头
            post.setHeader("Content-type", "application/x-www.form-urlencoded; charset=utf-8");
            if (!"".equals(token))
                post.setHeader("x-wlk-Authorization", token);
            // 构建消息实体
            StringEntity entity = new StringEntity(body, Charset.forName("UTF-8"));
            entity.setContentEncoding("UTF-8");
            // 发送Json格式的数据请求
            entity.setContentType("application/x-www.form-urlencoded");
            post.setEntity(entity);
            HttpResponse response = httpClient.execute(post);
            try {
                // 获取响应实体
                HttpEntity httpEntity = response.getEntity();
                if (entity != null) {
                    responseMessange = EntityUtils.toString(httpEntity);
                    System.out.println("响应"+responseMessange);
                    JSONObject parseObject = JSONObject.parseObject(responseMessange);
                    JSONArray array = parseObject.getJSONArray ("Data");
                    JSONObject o = (JSONObject) array.get(1);
                }
            } finally {
                ((Closeable) response).close();
            }
        } catch (Exception e) {
            LOG.error("post请求失败", e);
            System.out.println("post请求失败"+ e);
        } finally {
//            if (post != null) {
//                try {
                    post.releaseConnection();
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    LOG.error("关闭连接失败", e);
//                    System.out.println("关闭连接失败"+e);
//                }
//            }
        } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值