- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序 API 执行奇怪的行为。我的一个实时应用程序的 API 预计响应时间为 2 到 3 秒,但响应时间很长!
测试用例:
案例 1.
URL url = new URL("https://example.com");
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(10000);
int code = urlConnection.getResponseCode();
if (code != 200) {
throw new IOException("Invalid response from server: " + code);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
Log.e("data", line);
}
结果:
案例 2:
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Request.Builder requestBuilder = new Request.Builder()
.url("https://example.com")
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
Log.e("APi", "REQUEST: "+request.toString());
Call call1 = okHttpClient.newCall(request);
call1.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.e("APi", "APi Failed");
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Log.e("APi", "APi isSuccessful:" + response.isSuccessful());
Log.e("APi", "Response:" + response.body().string());
}
});
结果:
案例三:
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS).build();
Request.Builder requestBuilder = new Request.Builder()
.url("https://example.com")
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
Log.e("APi", "REQUEST: "+request.toString());
Call call1 = okHttpClient.newCall(request);
call1.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.e("APi", "APi Failed");
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
Log.e("APi", "APi isSuccessful:" + response.isSuccessful());
Log.e("APi", "Response:" + response.body().string());
}
});
结果:
注意:我害怕在应用程序中将 .connectTimeout(1, TimeUnit.SECONDS) 用作 1 秒它开始出现更多 api 故障并且看起来也不是正确的解决方案。
failed to connect to xxx.com/2001:4860:4802:36::15 (port XXX) from /2402:3a80:1b8f:ca21:847:21a8:5ffc:fc30 (port XXX) after 10000ms
包 okhttp3.internal.connection.RealConnection 类。尝试建立 connectSocket 但出现错误。
-> 添加“www”后www.example.com进入我的领域。okhttp 库的响应时间从 40 秒变为大约 12 秒。
最佳答案
很难看到全貌,因为你没有解释你是如何执行后续连接的。您完全有可能重用现有连接。默认情况下,HTTP/1.1 使连接保持 Activity 状态。
检查使用纯文本 HTTP 与 HTTPS 是否有区别也可能很有趣,因为 HTTPS 的连接设置成本更高。您的客户端还可以根据返回的证书进行更多验证。
如果更改主机名有如此大的不同,那么我肯定会查看系统上如何配置名称解析器。
我还会捕获网络流量 并查看耗时较长的内容。 tcpdump 是你的 friend 或者你总是可以考虑使用 wireshark
正如其他人所建议的,您可能还想禁用盒子上的 IPv6 堆栈以排除问题的根源。
关于android - OkHttpClient 与移动运营商问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63633799/
我有许多不同的 OkHttpClient 请求拦截器。每个拦截器只需要应用程序发出的网络请求的一个子集。到目前为止,我已经提出了两种可能的设计。我应该: 将每个拦截器附加到单个 OkHttpClien
我的应用程序 API 执行奇怪的行为。我的一个实时应用程序的 API 预计响应时间为 2 到 3 秒,但响应时间很长! 测试用例: 使用 WIFI = 是 与 Airtel 运营商合作 = 是 与 J
我正在尝试使用 okHttpClient 4.2.1 库注册用户,作为响应,我得到了一些垃圾值 Respone is �������������V*.MNN-.V�JK�)N�Q�r�S���rR�
我将 OkHttp3 与 proxy-vole 库一起使用来自动选择要使用的代理。就我而言,使用自动配置有 4 个可能的代理可用(都是相同的概念,只是负载平衡)。然而,似乎只有其中之一可以在没有显式身
我想要获得正文响应,但收到错误 403,尽管页面在 webview 和浏览器中打开。我的代码: OkHttpClient client = new OkHttpClient();
我有一个 Web 容器 (Glassfish),在其系统属性上为一个应用程序配置了代理。 现在我在同一个 Web 容器上有另一个应用程序,我想使用 OkHttpClient 通过代码自定义代理使用。
尝试了解我在传输到 OkHttpClient 时可以定义的最佳 Java ThreadPoolTaskExecutor(延迟方面)。目前我们的定义如下: 最大队列容量(将打开新线程)为 20
在我的第一个 Android 项目中,我创建了一个 onClick 事件来调用函数: public void doSomething(View v) { String result =
okHttpClient在出现如下flag时会重试多少次才放弃public OkHttpClient.Builder retryOnConnectionFailure(boolean retryOnC
只是想模拟 OkHttpClient 以使用 Mockito 进行测试。遗憾的是,OkHttpClient 是 Mockito 不支持的最终类。有没有其他可能进行 mock ,你们在 cicrumst
我正在使用改造和 okHttp 客户端连接到我的 rest api。当我在 Tomcat 上禁用基本身份验证时,一切正常。在 Tomcat 上启用 Basic Auth 后,我收到 404 Page
我在我的类中使用注入(inject)的 OkHttpClient 对象。我正在使用方法注入(inject)在我的客户端对象上设置一些拦截器,如下所示: @Inject private OkHttpCl
我有一个 Android 应用程序需要使用 REST 连接到服务器。我使用 Retrofit 2 来处理请求,效果很好。问题是当我想使用 SSL 连接时。使用开放式 ssl,我有一个 client.c
OkHttpClient 是否可以限制实时连接数?因此,如果达到限制,则不会选择和建立新连接? 我的应用同时启动多个连接。 最佳答案 连接数可在Dispatcher中配置,而不是在仅允许配置最大空闲连
我正在为 OkHttpClient 设置连接失败时重试选项。 client = new OkHttpClient(); client.setRetryOnConnectionFailure(true)
我最近升级到 OkHttp3 ,并注意到您无法再直接从客户端取消通过标签调用。这必须由应用程序现在处理。 在 CHANGELOG 中说明这里: Canceling batches of calls i
我想使用 OkHttpClient 加载一个 url,如果给定 url 的网站以 pdf 内容类型响应,我将继续下载 PDF,否则我想忽略该响应。 我的问题是,我是否需要做任何特殊的事情来关闭请求/响
enter image description here Okhttp 使用 OkHttpClent 发送请求。 我可以使用相同的 OkHttpClent 实例在不同的线程中请求吗? 最佳答案 是的。
我想使用PipedOutputStream和 PipedInputStream流响应主体。我不太确定它在多线程方面是否安全。将从不同的线程访问响应。 public Streamer execute()
我正在努力实现这个目标 这是/delete/{id} 如何在 java 中执行此操作? String id = "123"; OkHttpClient client = new OkHttpClien
我是一名优秀的程序员,十分优秀!