okhttp Stream Load 含认证请求重定向

http 请求 StarRocks Stream Load,遇到错误码:307

xml 复制代码
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.12.0</version>
</dependency>
java 复制代码
public static void main(String[] args) {
			OkHttpClient httpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                .writeTimeout(120, TimeUnit.SECONDS)
                .followRedirects(true)           // 默认 true,跟 301/302/303/307/308
                .followSslRedirects(true)        // 默认 true,跟 https↔https
                .build();

        try {
            String body = "{\"bid_cid\":1000,\"bid_realized_amount\":0,\"trend_side\":\"BUY\",\"ask_user_id\":11377,\"bid_id\":0,\"mtime\":1779138579000,\"ask_id\":0,\"volume\":19,\"ask_realized_amount\":0,\"buy_fee\":0,\"ask_cid\":1000,\"price\":2119.5,\"ctime\":1779138579000,\"id\":21023454,\"bid_user_id\":11377,\"sell_fee\":0,\"source_table\":\"co_trade_e_ethusdt\",\"binlogFile\":\"mysql-bin.038191\",\"binlogPosition\":31850563,\"op\":\"c\",\"ts\":\"2026-05-18 21:09:40\",\"dt\":\"2026-05-18\"}";

            // String url = "http://192.168.154.2:18040" + "/api/" + "ods"
            //        + "/" + "ods_co_trade_history" + "/_stream_load";
            String url = "http://192.168.154.2:8030" + "/api/" + "ods"
                    + "/" + "ods_co_trade_history" + "/_stream_load";
            RequestBody requestBody = RequestBody.create(body, MediaType.parse("application/json; charset=utf-8"));

            Request request = new Request.Builder()
                    .url(url)
                    .put(requestBody)
                    .addHeader("label", "flink_stream_load_label1")
                    .addHeader("format", "json")
                    .addHeader("strip_outer_array", "false")
                    .addHeader("Expect", "100-continue")
                    .addHeader("Authorization",
                            Credentials.basic("root", "123456"))
                    .build();

            Response response = httpClient.newCall(request).execute();
            String respBody = response.body() != null
                    ? response.body().string() : "";
            boolean ok = response.isSuccessful();

            System.out.println(respBody);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
}
问题记录

no valid Basic authorization

bash 复制代码
{
    "TxnId": -1,
    "Label": "flink_ods_0_1779181518093_ods_co_trade_history",
    "Db": "ods",
    "Table": "ods_co_trade_history",
    "Status": "Fail",
    "Message": "no valid Basic authorization",
    "NumberTotalRows": 0,
    "NumberLoadedRows": 0,
    "NumberFilteredRows": 0,
    "NumberUnselectedRows": 0,
    "LoadBytes": 0,
    "LoadTimeMs": 0,
    "BeginTxnTimeMs": 0,
    "StreamLoadPlanTimeMs": 0,
    "ReadDataTimeMs": 0,
    "WriteDataTimeMs": 0,
    "CommitAndPublishTimeMs": 0
}

当重定向到不同主机时,OkHttp 会主动剥离 Authorization 头(安全策略)。

解决: 自定义 Interceptor 强制保留认证头

java 复制代码
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(new RedirectAuthInterceptor("root", "123456"))
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                .writeTimeout(120, TimeUnit.SECONDS)
                .followRedirects(true)           // 默认 true,跟 301/302/303/307/308
                .followSslRedirects(true)        // 默认 true,跟 https↔https
                .build();


		/**
     * 关键:强制在重定向后保留 Authorization 头
     */
    static class RedirectAuthInterceptor implements Interceptor {
        private final String authHeader;

        RedirectAuthInterceptor(String user, String password) {
            this.authHeader = Credentials.basic(user, password);
        }

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();
            Request request = original.newBuilder()
                    .header("Authorization", authHeader)
                    .build();
            return chain.proceed(request);
        }
    }
json 复制代码
{
    "TxnId": 39821,
    "Label": "flink_stream_load_label2",
    "Db": "ods",
    "Table": "ods_co_trade_history",
    "Status": "Success",
    "Message": "OK",
    "NumberTotalRows": 1,
    "NumberLoadedRows": 1,
    "NumberFilteredRows": 0,
    "NumberUnselectedRows": 0,
    "LoadBytes": 418,
    "LoadTimeMs": 76,
    "BeginTxnTimeMs": 2,
    "StreamLoadPlanTimeMs": 5,
    "ReadDataTimeMs": 0,
    "WriteDataTimeMs": 49,
    "CommitAndPublishTimeMs": 19
}
错误码:307

.followRedirects(false)

bash 复制代码
Response{protocol=http/1.1, code=307, message=Temporary Redirect, url=http://192.168.154.2:8030/api/ods/ods_co_trade_history/_stream_load}

.followRedirects(true)

bash 复制代码
Response{protocol=http/1.1, code=200, message=OK, url=http://192.168.154.2:18040/api/ods/ods_co_trade_history/_stream_load}
相关推荐
MandalaO_O8 小时前
SpringMVC 响应返回值类型笔记
okhttp
镜舟科技9 小时前
镜舟科技 CTO 陈鹏:从实时分析到统一的数据分析与智能平台
starrocks·数据分析·镜舟科技·data agent·mip·统一数据·业务语义
小羊没烦恼!3 天前
Ajax与JSON的一些总结
android·java·前端·数据库·mysql·算法·okhttp
脉动数据行情4 天前
Java OkHttp 对接国际期货批量行情 SpringBoot 定时落库实践
java·spring boot·okhttp
StarRocks_labs7 天前
StarRocks 接入 Paimon C++:让湖上查询进入原生执行链路
starrocks·c++·sql·jni·paimon·auto·湖上查询
牧瀬クリスだ10 天前
Ajax入门:从原生XHR到axios实战
java·spring boot·ajax·okhttp
墨天梦13 天前
B15_HTTP解析与OkHttp拦截器
网络协议·http·okhttp
镜舟科技15 天前
从 StarRocks 到 MIP:数据平台的下一步是“理解”
starrocks·sql·ai·prompt·agent·context·mip
docsz23 天前
StarRocks 4.X实战
starrocks