【发布时间】:2018-10-23 02:37:48
【问题描述】:
我需要从站点获取 XML 文件。我正在学习使用改造。
我需要通过 "X-AppId" 标头发出请求并附加我的 API 密钥。它应该是这样的:
X-AppId: my key.
如果我从浏览器执行此操作,我会得到答案。 通过改造,我获得了访问权限
错误 403 禁止代码 = 403,消息 = 禁止,url = https://
告诉我如何正确实现从服务器code = 200接收答案
这是我的实现:
public interface myAPIinterface {
@GET("/api/ru/index/route/?from=Minsk&to=Warsaw")
Call<Routes> getProducts();
}
这是我输出到日志的活动:
private void getProducts(){
final ProgressDialog loading = ProgressDialog.show(this,"Fetching Data","Please wait...",false,false);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Log.d(TAG, "getProducts");
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);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://rasp.rw.by")
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
myAPIinterface api = retrofit.create(myAPIinterface.class);
Call<Routes> call = api.getProducts();
call.enqueue(new Callback<Routes>() {
@Override
public void onResponse(@NonNull Call<Routes> call, @NonNull Response<Routes> response) {
Log.d(TAG, "onResponse");
Log.d(TAG, String.valueOf(kk));
Log.d(TAG, String.valueOf(response));
loading.dismiss();}
@Override
public void onFailure(Call<Routes> call, Throwable throwable) {
loading.dismiss();
Log.d(TAG, "onFailure" + throwable);
}
});
这是一个日志:
响应{protocol=http/1.1, code=403, message=Forbidden, url=https://rasp.rw.by/api/ru/index/route/?from=Minsk&to=Warsaw}
如果我使用没有标题的第三方网站,我会得到 200 的响应,没有问题。在这种情况下我做错了什么?谢谢。
【问题讨论】: