发送http和https请求工具类 Json封装数据

在一些业务中我们可要调其他的接口(第三方的接口) 这样就用到我接下来用到的工具类。
用这个类需要引一下jar包的座标

       <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

引好座标后就可以使用这个工具类了;

Connection.Response post = HttpUtils.post("http://localhost:10090/Controller/httpToJson",str);
String body = post.body();
System.out.println("响应报文:"+body);

str代表你需要传的参数 一般是json类型的数据

访问成功后一般会返回你一个json的数据,但是这个数据有一些问题,它会进行转译。我把这个字符串进行了一些处理。

{"result":"{\"FoodID\":\"A66J54DW9IKJ75U3\",\"FoodName\":\"桃子\",\"FoodBrand\":\"龙冠\",\"FoodPlace\":\"吉林\",\"FoodText\":\"四打击我家的娃哦哦囧\",\"FoodImg\":\"sdasdasdasd\",\"FoodProInfo\":[{\"ProjectName\":\"浇水\",\"UserName\":\"周锐\",\"OperationDescribe\":\"给桃子树浇水茁壮成长\",\"ImgUrl\":\"asdlpalplpd23\",\"OperationTm\":\"20200616103856\"}],\"FoodPackInfo\":null,\"FoodDetectionInfo\":null,\"FoodLogInfo\":null}","code":0,"message":"查询智能合约成功"}

对这个串进行替换等一些操作,然后把它转成json对象。通过json工具类把它自动封装到实体类型中,
(在进行封装实体的时候一定要注意,json串的属性和实体的属性要完全相同)否则会报错的(避免入坑)

 Connection.Response post = HttpUtils.post("http://localhost:10090/fabricController/queryChaincode",str);
            String body = post.body();
            String replace = body.replace("\\\"", "\"");
            String replace1 = replace.replace(":\"{", ":{");
            String replace2 = replace1.replace("}\",", "},");
            String replace3 = replace2.replace("{\"result\":", "");
            String replace4 = replace3.replace(",\"code\":0,\"message\":\"查询智能合约成功\"}", "");            
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(replace4);
            FoodAllInfo foodAllInfo = JSON.parseObject(replace4, FoodAllInfo.class);


package com.gblfy.order.utils;

import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

import javax.net.ssl.*;
import java.io.*;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HttpUtils {

    /**
     * 请求超时时间
     */
    private static final int TIME_OUT = 120000;

    /**
     * Https请求
     */
    private static final String HTTPS = "https";

    /**
     * 发送Get请求
     *
     * @param url 请求URL
     * @return 服务器响应对象
     * @throws IOException
     */
    public static Response get(String url) throws IOException {
        return get(url, null);
    }

    /**
     * 发送Get请求
     *
     * @param url     请求URL
     * @param headers 请求头参数
     * @return 服务器响应对象
     * @throws IOException
     */
    public static Response get(String url, Map<String, String> headers) throws IOException {
        if (null == url || url.isEmpty()) {
            throw new RuntimeException("The request URL is blank.");
        }

        // 如果是Https请求
        if (url.startsWith(HTTPS)) {
            getTrust();
        }
        Connection connection = Jsoup.connect(url);
        connection.method(Connection.Method.GET);
        connection.timeout(TIME_OUT);
        connection.ignoreHttpErrors(true);
        connection.ignoreContentType(true);

        if (null != headers) {
            connection.headers(headers);
        }

        Response response = connection.execute();
        return response;
    }

    /**
     * 发送JSON格式参数POST请求
     *
     * @param url    请求路径
     * @param params JSON格式请求参数
     * @return 服务器响应对象
     * @throws IOException
     */
    public static Response post(String url, String params) throws IOException {
        return doPostRequest(url, null, null, null, params);
    }

    /**
     * 发送JSON格式参数POST请求
     *
     * @param url     请求路径
     * @param headers 请求头中设置的参数
     * @param params  JSON格式请求参数
     * @return 服务器响应对象
     * @throws IOException
     */
    public static Response post(String url, Map<String, String> headers, Map<String, String> params,String flag) throws IOException {
        return doPostRequest(url,headers,params,null,null);

    }

    /**
     * 字符串参数post请求
     *
     * @param url      请求URL地址
     * @param paramMap 请求字符串参数集合
     * @return 服务器响应对象
     * @throws IOException
     */
    public static Response post(String url, Map<String, String> headers) throws IOException {
        return doPostRequest(url, headers, null, null, null);

    }

    /**
     * 带上传文件的post请求
     *
     * @param url      请求URL地址
     * @param paramMap 请求字符串参数集合
     * @param fileMap  请求文件参数集合
     * @return 服务器响应对象
     * @throws IOException
     */
    public static Response post(String url, Map<String, String> paramMap, Map<String, File> fileMap)
            throws IOException {
        return doPostRequest(url, null, paramMap, fileMap, null);
    }

    /**
     * 执行post请求
     *
     * @param url      请求URL地址
     * @param paramMap 请求字符串参数集合
     * @param fileMap  请求文件参数集合
     * @return 服务器响应对象
     * @throws IOException
     */
    private static Response doPostRequest(String url, Map<String, String> headers, Map<String, String> paramMap,
                                          Map<String, File> fileMap, String jsonParams) throws IOException {
        if (null == url || url.isEmpty()) {
            throw new RuntimeException("The request URL is blank.");
        }
        // 如果是Https请求
        if (url.startsWith(HTTPS)) {
            getTrust();
        }
        Connection connection = Jsoup.connect(url);
        connection.method(Connection.Method.POST);
        connection.timeout(TIME_OUT);
        connection.ignoreHttpErrors(true);
        connection.ignoreContentType(true);
        if (null != headers) {
            connection.headers(headers);
        }
        // 收集上传文件输入流,最终全部关闭
        List<InputStream> inputStreamList = null;
        try {
            // 添加文件参数
            if (null != fileMap && !fileMap.isEmpty()) {
                inputStreamList = new ArrayList<InputStream>();
                InputStream in = null;
                File file = null;
                Set<Entry<String, File>> set = fileMap.entrySet();
                for (Entry<String, File> e : set) {
                    file = e.getValue();
                    in = new FileInputStream(file);
                    inputStreamList.add(in);
                    connection.data(e.getKey(), file.getName(), in);
                }
            }
            // 设置请求体为JSON格式内容
            else if (null != jsonParams && !jsonParams.isEmpty()) {
                connection.header("Content-Type", "application/json;charset=UTF-8");
                connection.requestBody(jsonParams);
            }
            // 普通表单提交方式
            else {
                connection.header("Content-Type", "application/x-www-form-urlencoded");
            }
            // 添加字符串类参数
            if (null != paramMap && !paramMap.isEmpty()) {
                connection.data(paramMap);
            }
            Response response = connection.execute();
            return response;
        } catch (FileNotFoundException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        }
        // 关闭上传文件的输入流
        finally {
            if (null != inputStreamList) {
                for (InputStream in : inputStreamList) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 获取服务器信任
     */
    private static void getTrust() {
        try {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[] { new X509TrustManager() {

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            } }, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章