原创

java获取短链对应长链中的指定参数

1、引入pom

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.8.1</version>
</dependency>

2、通用工具类

package com.timevale.epaas.doctemplate.test.utils;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @description http请求工具类
 * @date 2023-09-06 15:22
 * @author chen xing
 */
@Component
public class OkhttpUtil {
    private static OkHttpClient okHttpClient;

    @Autowired
    public OkhttpUtil(OkHttpClient okHttpClient) {
        OkhttpUtil.okHttpClient = okHttpClient;
    }

    public static HttpUrl getRedirectUrl(String url) throws IOException {
        Request.Builder builder = new Request.Builder().url(url);
        builder.addHeader("Content-Type", "application/json");
        Request request = builder.build();
        Response response = okHttpClient.newCall(request).execute();
        return response.request().url();
    }
    /**
     * @author chen xing
     * @description 获取对应长连接中的param参数
     * @param url
     * @param params
     * @return java.lang.String
     * @date 2023-09-06 17:48
     */
    public static String getRedirectUrlParam(String url, String params) throws IOException {
        HttpUrl redirectUrl = getRedirectUrl(url);
        return redirectUrl.queryParameter(params);
    }
}

3、使用案例

String fillUrl="https://testt.tsign.cn/iJlko7d";
String encryptionparam = OkhttpUtil.getRedirectUrlParam(fillUrl, "encryption");


目前短链对应的长链是 
http://epaas-template-front-epaas-doc-template.projectk8s.tsign.cn/file-template/set?serviceId=fqy_0829&encryption=NXQQsWlAS5_AGjAx393Hzz1XcFgYtDcUr-fOVAAU6TG_TSU4aJWKjWEMtejCOWzqCmHNmwSCCC-mTZvdS3SNyA&auth=free
正文到此结束
本文目录