You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Retrofit+OkHttp3添加X-AppId请求头遇403问题求助

解决Retrofit请求返回403 Forbidden的问题

嘿,我帮你找到了两处关键配置错误,这就是你用Retrofit请求时拿到403的原因:

1. 请求头键名多了多余的冒号

你在拦截器里添加请求头时写的是:

.addHeader("X-AppId:", "97377f7b702d7198e47a2bf12eec74")

这里的键名X-AppId:多了一个冒号,HTTP请求头的键名本身不需要带冒号——冒号是HTTP协议里用来分隔键和值的符号,Retrofit会自动处理这个分隔逻辑,你只需要传入干净的键名即可。

2. 配置的OkHttpClient没有和Retrofit绑定

你创建了带有请求头拦截器的OkHttpClient.Builder,但在构建Retrofit实例时,并没有把这个配置好的Client传递进去!这就导致Retrofit默认使用了不带任何自定义配置的OkHttpClient,自然没带上你需要的X-AppId请求头。

修正后的完整代码

接口定义(优化参数传递方式,更灵活)

public interface myAPIinterface { 
    @GET("/api/ru/index/route/")
    Call<Routes> getProducts(@Query("from") String from, @Query("to") String to); 
}

(把硬编码在URL里的参数改成@Query注解形式,后续修改出发地、目的地会更方便)

Activity中的请求代码

private void getProducts(){ 
    final ProgressDialog loading = ProgressDialog.show(this,"Fetching Data","Please wait...",false,false); 
    Log.d(TAG, "getProducts"); 

    // 1. 配置OkHttpClient,添加请求头拦截器
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
    httpClient.addInterceptor(new Interceptor() { 
        @Override 
        public okhttp3.Response intercept(Chain chain) throws IOException { 
            Request request = chain.request() 
                    .newBuilder() 
                    .addHeader("X-AppId", "97377f7b702d7198e47a2bf12eec74") // 去掉键名里的冒号
                    .build(); 
            return chain.proceed(request); 
        } 
    }); 

    // 2. 构建Retrofit时,传入配置好的OkHttpClient(关键步骤)
    Retrofit retrofit = new Retrofit.Builder() 
            .baseUrl("https://rasp.rw.by") 
            .client(httpClient.build()) 
            .addConverterFactory(SimpleXmlConverterFactory.create()) 
            .build(); 

    myAPIinterface api = retrofit.create(myAPIinterface.class); 
    // 用动态参数发起请求
    Call<Routes> call = api.getProducts("Minsk", "Warsaw"); 
    call.enqueue(new Callback<Routes>() { 
        @Override 
        public void onResponse(@NonNull Call<Routes> call, @NonNull Response<Routes> response) { 
            Log.d(TAG, "onResponse"); 
            Log.d(TAG, "Response code: " + response.code()); 
            loading.dismiss();
            // 这里可以处理返回的Routes数据
        } 

        @Override 
        public void onFailure(Call<Routes> call, Throwable throwable) { 
            loading.dismiss(); 
            Log.d(TAG, "onFailure: " + throwable.getMessage()); 
        } 
    }); 
}

额外调试建议

如果想确认请求头是否正确带上,可以添加OkHttp的日志拦截器,方便查看请求细节:

// 添加日志拦截器到OkHttpClient
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS); // 只打印请求头和响应头
httpClient.addInterceptor(logging);

记得在build.gradle里添加对应依赖:

implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3' // 可替换为最新版本

这样修改后,你的Retrofit请求就会正确带上X-AppId请求头,应该就能拿到200的响应啦!

内容的提问来源于stack exchange,提问作者Романыч

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:23:41