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}