httpclient post方法【亲测可用】

版权声明:文章来源网络,版权归作者本人所有,如侵犯到原作者权益,请与我们联系删除或授权事宜,如果有误,请联系作者更改,谢谢,本人微信:void666666 https://blog.csdn.net/wt520it/article/details/86551236
很多代码都是根据需求来搞代码的。很早之前的就是给app写的接口,主要项目采用的是基础RMI的项目,服务层不能暴露给外网访问,所以需要给App写转发接口,让App调用其他平台的(内网)接口,重定向很简单的的,但是需要接受其他平台的返回的json。

在这里插入图片描述

主要使用的httpClient的post方法:直接上代码

添加依赖:

<!--httpcomponents的包-->
<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>

<!--JSONobject的包-->
 <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
 </dependency>

工具类:

package com.upiweb.common.util;

import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;


/**
 * 调用的是接口
 */

public class SendSignHttpsClient {


    /**
     * post请求以及参数是json
     *
     * @param url
     * @param jsonParams
     * @return
     */
    public static JSONObject doPostForJson(String url, String jsonParams) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonObject = null;
        HttpPost httpPost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().
                setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
                .setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
        httpPost.setConfig(requestConfig);
        httpPost.setHeader("Content-Type", "application/json");
        try {
            httpPost.setEntity(new StringEntity(jsonParams, ContentType.create("application/json", "utf-8")));
            System.out.println("request parameters" + EntityUtils.toString(httpPost.getEntity()));
            System.out.println("httpPost:" + httpPost);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                System.out.println("result:" + result);
                jsonObject = JSONObject.fromObject(result);
                return jsonObject;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return jsonObject;
        }
    }


}

测试代码:


	@RequestMapping(value = "get",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JSONObject get(@RequestBody String json) {
        LOGGER.info("test");
        JSONObject jsonObject = SendSignHttpsClient.doPostForJson(+"localost:8080/demo/test",json);
        return jsonObject;
    }

在实战中学习,在快乐中成长

猜你喜欢

转载自blog.csdn.net/wt520it/article/details/86551236
今日推荐