调用5G核心网(明文http2)

调用5G核心网(明文http2)

第一步,环境准备:

1、下载alpn-boot的jar包并引入到项目中(下载地址(alpn-boot-8.1.13.v20181017.jar)

  Maven Repository: org.mortbay.jetty.alpn » alpn-boot » 8.1.13.v20181017 (mvnrepository.com))

2、IDEA项目启动时,设置启动参数(后面为alpn-boot-8.1.13.v20181017.jar的绝对路径)

-Xbootclasspath/p:D:\maven\repository\org\mortbay\jetty\alpn\alpn-boot\8.1.13.v20181017\alpn-boot-8.1.13.v20181017.jar

3、JAR启动JVM时加参数java -Xbootclasspath/p:<path_to_alpn_boot_jar>

第二步,升级okHttpClient包到4.1.0:

<!-- http2 -->
<dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
      <version>4.1.0</version>
</dependency>

第三步,编写测试程序:

package com.example.demo.util;

import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.ConnectionPool;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * http2工具类
 *
 * @author li.zhm
 */
@Slf4j
public class Http2Util {

    /***
     * 调用api post接口
     *
     * @author li.zhm
     * @param url
     * @param jsonObject
     * @param ntType
     * @history <author>  <time>  <version>  <desc>
     */
    public static void sendPost(String url, JSONObject jsonObject, String ntType) {
        log.info("请求post地址(转义):{}", URLUtil.encode(url, "utf-8"));
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        List<Protocol> protocolList = new ArrayList<>();
        protocolList.add(Protocol.H2_PRIOR_KNOWLEDGE);
        OkHttpClient.Builder builder =
                new okhttp3.OkHttpClient.Builder().readTimeout(60, TimeUnit.SECONDS).connectTimeout(60,
                        TimeUnit.SECONDS).writeTimeout(120, TimeUnit.SECONDS).connectionPool(new ConnectionPool())
                        .protocols(protocolList);
        OkHttpClient client = builder.build();

        RequestBody requestBody = RequestBody.create(mediaType, jsonObject.toString());
        okhttp3.Request.Builder httpRequestBuilder =
                new Request.Builder().url(URLUtil.encode(url, "utf-8")).post(requestBody);
        Request request = httpRequestBuilder.build();
        try {
            // 同步
//            Response response = client.newCall(request).execute();
//            if(response.isSuccessful()){
//                body = response.body().string();
//                log.info("响应body:{}", body);
//            }else{
//                log.error("调用http2 post失败,response.code={}", response.code());
//            }

            // 异步
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    log.error("调用http2 post异常,{}", e.toString());
                }
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String body = Objects.requireNonNull(response.body()).string();
                        log.info("调用http2 post响应body:{}", body);
                        if (body != null && !"".equals(body)) {
                            NfResponseUtil nfResponseUtil = new NfResponseUtil();
                            nfResponseUtil.nfResponse(ntType, body);
                        }
                    } else {
                        log.error("调用http2 post失败,response.code={}", response.code());
                    }
                }
            });
        } catch (Exception e) {
            log.error("调用http2 post异常,{}", e.toString());
            e.printStackTrace();
        }
    }
}

 

posted @ 2022-10-24 11:19  整合侠  阅读(272)  评论(0编辑  收藏  举报