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}
相关推荐
CYY954 天前
OkHttp 和 Retrofit 封装使用
okhttp·retrofit
CYY955 天前
OkHttp 的使用
okhttp
朝星16 天前
Android开发[14]:网络优化之OkHttp
android·okhttp·kotlin
之歆18 天前
Promise 基础技术深度解析:从回调地狱到链式调用
前端·okhttp·promise
之歆18 天前
Ajax 基础技术深度解析:XHR 从入门到跨域
前端·ajax·okhttp
YHHLAI19 天前
Ajax — 异步数据交互
ajax·okhttp·交互
Aurora_Dawn_yy20 天前
单机部署数据同步_jdk,mysql,kafka,flink,zookeeper,达梦,starrocks
大数据·linux·starrocks·zookeeper·达梦
阿里云大数据AI技术21 天前
阿里云 EMR Serverless StarRocks Skills 正式发布
starrocks·阿里云·serverless·agent·skill
镜舟科技24 天前
镜舟科技出席 HPE 新品发布会,携手打造“Lakehouse + AI”智能数据底座
starrocks·数据分析·ai agent·lakehouse·hpe
镜舟科技24 天前
从 Prompt 到 Context Engineering:如何用 StarRocks 构建 AI Agent 的实时上下文引擎?
starrocks·大模型·prompt·ai agent·数据基础设施·上下文工程