Java关注"Please enable JS and disable any ad blocker"
Java Okhttp3 "Please enable JS and disable any ad blocker"
我正在尝试获取获取 json 文件的请求,但是一旦我这样做,我得到的日志是它没有看到 json 文件。事实上,它只看到下面的日志。
为了获取 Json 文件的信息,我是否应该得到除此之外的其他响应?
这是 link 我想从以下位置获得请求:
https://www.footlocker.nl/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json
jan. 10, 2021 11:26:59 P.M. okhttp3.internal.platform.Platform log
INFO: <html><head><title>footlocker.nl</title><style>#cmsg{animation: A 1.5s;}@keyframes A{0%{opacity:0;}99%{opacity:0;}100%{opacity:1;}}</style></head><body style="margin:0"><p id="cmsg">Please enable JS and disable any ad blocker</p><script>var dd={'cid':'AHrlqAAAAAMA_bz9MUAng5wAUHCDZw==','hsh':'A55FBF4311ED6F1BF9911EB71931D5','t':'fe','s':17434,'host':'geo.captcha-delivery.com'}</script><script src="https://ct.captcha-delivery.com/c.js"></script></body></html>
这是我的代码:
public FootlockerV2() {
tester();
}
public void tester() {
var loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//httpClient maken.
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(chain -> {
Request request = chain.request();
Request.Builder newRequest = request.newBuilder()
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
.header("authority", "www.footlocker.nl")
.header("scheme", "https")
.header("cache-control", "no-cache")
.header("path", "/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json")
.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
return chain.proceed(newRequest.build());
}).addInterceptor(loggingInterceptor);
Retrofit retrofit = new Retrofit.Builder()
.client(httpClient.build())
.baseUrl(Api.base)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<List<Article>> call = api.getArticles();
call.enqueue(new Callback<List<Article>>() {
@Override
public void onResponse(Call<List<Article>> call, Response<List<Article>> response) {
List<Article> articles = response.body();
if (articles != null) {
for (Article article : articles) {
System.out.println(response.body().toString());
System.out.println(article.getName() + "\n");
}
}
}
@Override
public void onFailure(Call<List<Article>> call, Throwable t) {
}
});
}`
处理此类响应或多或少是微不足道的,通常遵循以下模式:
复制生成的curl
命令
在调试器网络选项卡中打开网络请求(假设您在 Google Chrome/Chromium 上,如果我已正确解析 User-Agent
header),并且使用“Copy as cURL”菜单项复制它。复制后,double-check 生成的命令在终端中运行良好。
Trim 关闭 curl
命令中不必要的参数
我通常会为此类实验创建一个快速的小型 git 存储库,我会在其中提交每一个成功的步骤,然后首先将命令放入可执行 bash 脚本,然后快速 运行 .你trim越多,curl
命令就变得越小,这样你的curl
命令就尽可能小。这是我得到的:
#!/bin/bash
curl 'https://www.footlocker.nl/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36' \
-H 'accept-language: en-US,en;q=0.9' \
--compressed
这是满足您要求的最小 可行命令。现在启用 -v
开关,以便您可以检查 curl
实际发送到服务器的内容:
#!/bin/bash
curl -v 'https://www.footlocker.nl/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36' \
-H 'accept-language: en-US,en;q=0.9' \
--compressed
您可能会看到如下内容:
> GET /INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json HTTP/2
> Host: www.footlocker.nl
> Accept: */*
> Accept-Encoding: deflate, gzip
> user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36
> accept-language: en-US,en;q=0.9
因为你好像用的是Windows系统,不一定需要bash
,cmd
应该没问题,但是要确保Chrome/Chromium生成一个cmd
兼容curl
命令。
将 curl
请求模式转换为 Retrofit 拦截器
请注意,如果没有实际映射到 Accept-Encoding: deflate, gzip
的 --compressed
,它就无法工作。
okhttp 3: how to decompress gzip/deflate response manually using Java/Android
final class GunzipRequestInterceptor
implements Interceptor {
private static final Interceptor instance = new GunzipRequestInterceptor();
private GunzipRequestInterceptor() {
}
static Interceptor getInstance() {
return instance;
}
@Override
public Response intercept(final Chain chain)
throws IOException {
return gunzip(chain.proceed(chain.request()));
}
private static Response gunzip(final Response response) {
if ( response.body() == null ) {
return response;
}
final String contentEncoding = response.headers().get("Content-Encoding");
if ( !contentEncoding.equals("gzip") ) {
return response;
}
final Headers headers = response.headers().newBuilder().build();
final ResponseBody body = new RealResponseBody(headers, Okio.buffer(new GzipSource(response.body().source())));
return response.newBuilder()
.headers(headers)
.body(body)
.build();
}
}
interface IFootLockerClient {
@GET("/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/\"%20+%20\"en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json")
Call<JsonElement> get();
}
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.footlocker.nl/")
.addConverterFactory(GsonConverterFactory.create())
.client(new OkHttpClient.Builder()
.addInterceptor(GunzipRequestInterceptor.getInstance())
.addInterceptor(chain -> chain.proceed(chain.request()
.newBuilder()
.header("Accept", "*/*")
.header("Accept-Encoding", "gzip")
.header("Accept-Language", "en-US,en;q=0.9")
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36")
.build()
))
.build()
)
.build();
final IFootLockerClient client = retrofit.create(IFootLockerClient.class);
final Call<JsonElement> jsonElement = client.get();
System.out.println(jsonElement.execute().body());
这应该可以工作并将 well-parsed JSON 响应根元素写入标准输出。
我正在尝试获取获取 json 文件的请求,但是一旦我这样做,我得到的日志是它没有看到 json 文件。事实上,它只看到下面的日志。
为了获取 Json 文件的信息,我是否应该得到除此之外的其他响应?
这是 link 我想从以下位置获得请求: https://www.footlocker.nl/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json
jan. 10, 2021 11:26:59 P.M. okhttp3.internal.platform.Platform log
INFO: <html><head><title>footlocker.nl</title><style>#cmsg{animation: A 1.5s;}@keyframes A{0%{opacity:0;}99%{opacity:0;}100%{opacity:1;}}</style></head><body style="margin:0"><p id="cmsg">Please enable JS and disable any ad blocker</p><script>var dd={'cid':'AHrlqAAAAAMA_bz9MUAng5wAUHCDZw==','hsh':'A55FBF4311ED6F1BF9911EB71931D5','t':'fe','s':17434,'host':'geo.captcha-delivery.com'}</script><script src="https://ct.captcha-delivery.com/c.js"></script></body></html>
这是我的代码:
public FootlockerV2() {
tester();
}
public void tester() {
var loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//httpClient maken.
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(chain -> {
Request request = chain.request();
Request.Builder newRequest = request.newBuilder()
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
.header("authority", "www.footlocker.nl")
.header("scheme", "https")
.header("cache-control", "no-cache")
.header("path", "/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json")
.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
return chain.proceed(newRequest.build());
}).addInterceptor(loggingInterceptor);
Retrofit retrofit = new Retrofit.Builder()
.client(httpClient.build())
.baseUrl(Api.base)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<List<Article>> call = api.getArticles();
call.enqueue(new Callback<List<Article>>() {
@Override
public void onResponse(Call<List<Article>> call, Response<List<Article>> response) {
List<Article> articles = response.body();
if (articles != null) {
for (Article article : articles) {
System.out.println(response.body().toString());
System.out.println(article.getName() + "\n");
}
}
}
@Override
public void onFailure(Call<List<Article>> call, Throwable t) {
}
});
}`
处理此类响应或多或少是微不足道的,通常遵循以下模式:
复制生成的curl
命令
在调试器网络选项卡中打开网络请求(假设您在 Google Chrome/Chromium 上,如果我已正确解析 User-Agent
header),并且使用“Copy as cURL”菜单项复制它。复制后,double-check 生成的命令在终端中运行良好。
Trim 关闭 curl
命令中不必要的参数
我通常会为此类实验创建一个快速的小型 git 存储库,我会在其中提交每一个成功的步骤,然后首先将命令放入可执行 bash 脚本,然后快速 运行 .你trim越多,curl
命令就变得越小,这样你的curl
命令就尽可能小。这是我得到的:
#!/bin/bash
curl 'https://www.footlocker.nl/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36' \
-H 'accept-language: en-US,en;q=0.9' \
--compressed
这是满足您要求的最小 可行命令。现在启用 -v
开关,以便您可以检查 curl
实际发送到服务器的内容:
#!/bin/bash
curl -v 'https://www.footlocker.nl/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json' \
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36' \
-H 'accept-language: en-US,en;q=0.9' \
--compressed
您可能会看到如下内容:
> GET /INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/%22%20+%20%22en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json HTTP/2
> Host: www.footlocker.nl
> Accept: */*
> Accept-Encoding: deflate, gzip
> user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36
> accept-language: en-US,en;q=0.9
因为你好像用的是Windows系统,不一定需要bash
,cmd
应该没问题,但是要确保Chrome/Chromium生成一个cmd
兼容curl
命令。
将 curl
请求模式转换为 Retrofit 拦截器
请注意,如果没有实际映射到 Accept-Encoding: deflate, gzip
的 --compressed
,它就无法工作。
okhttp 3: how to decompress gzip/deflate response manually using Java/Android
final class GunzipRequestInterceptor
implements Interceptor {
private static final Interceptor instance = new GunzipRequestInterceptor();
private GunzipRequestInterceptor() {
}
static Interceptor getInstance() {
return instance;
}
@Override
public Response intercept(final Chain chain)
throws IOException {
return gunzip(chain.proceed(chain.request()));
}
private static Response gunzip(final Response response) {
if ( response.body() == null ) {
return response;
}
final String contentEncoding = response.headers().get("Content-Encoding");
if ( !contentEncoding.equals("gzip") ) {
return response;
}
final Headers headers = response.headers().newBuilder().build();
final ResponseBody body = new RealResponseBody(headers, Okio.buffer(new GzipSource(response.body().source())));
return response.newBuilder()
.headers(headers)
.body(body)
.build();
}
}
interface IFootLockerClient {
@GET("/INTERSHOP/static/FLE/Footlocker-Site/-/Footlocker/\"%20+%20\"en_US/Release-Calendar/Launchcalendar/launchdata/launchcalendar_feed_all.json")
Call<JsonElement> get();
}
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.footlocker.nl/")
.addConverterFactory(GsonConverterFactory.create())
.client(new OkHttpClient.Builder()
.addInterceptor(GunzipRequestInterceptor.getInstance())
.addInterceptor(chain -> chain.proceed(chain.request()
.newBuilder()
.header("Accept", "*/*")
.header("Accept-Encoding", "gzip")
.header("Accept-Language", "en-US,en;q=0.9")
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36")
.build()
))
.build()
)
.build();
final IFootLockerClient client = retrofit.create(IFootLockerClient.class);
final Call<JsonElement> jsonElement = client.get();
System.out.println(jsonElement.execute().body());
这应该可以工作并将 well-parsed JSON 响应根元素写入标准输出。