【问题标题】:Retrofit, OkHttp3 and Spring occasional error The request was rejected because the URL contained a potentially malicious String ";"Retrofit, OkHttp3 和 Spring 偶发错误请求被拒绝,因为 URL 包含潜在的恶意字符串“;”
【发布时间】:2021-11-17 07:08:23
【问题描述】:

我们遇到了网络错误,但不确定是什么原因造成的。我们从 Android 应用上传数据,大多数情况下它运行良好。

使用接口通过 Retrofit 和 OKHttp3 发送数据:

private static String BASE_URL = "https://backend.ourapp.com/api/";
    private static String cert1 = ...;
    private static String cert2 = ...;
    private static String cert3 = ...;

    public static Retrofit retrofit;

    static Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    static CertificatePinner certificatePinner = new CertificatePinner.Builder()
            .add("backend.ourapp.com", cert1)
            .add("backend.ourapp.com", cert2)
            .add("backend.ourapp.com", cert3)
            .build();

    public static HttpLoggingInterceptor logging =
            new HttpLoggingInterceptor()
                    .setLevel(HttpLoggingInterceptor.Level.BODY);

public static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(180, TimeUnit.SECONDS)
            .connectTimeout(180, TimeUnit.SECONDS)
            .certificatePinner(certificatePinner)
            .addInterceptor(logging)
            .build();


    public static Retrofit getClient(String url) {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(okHttpClient)
                    .build();
        }
        return retrofit;
    }

    public static ApiInterface apiInterface() {
        retrofit = null;
        return ApiClient.getClient(BASE_URL).create(ApiInterface.class);
    }

但是突然之间,我们开始随机出现问题。来自后端日志的错误是:

ERROR org.apache.juli.logging.DirectJDKLog [http-nio-7200-exec-1] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL contained a potentially malicious String ";"

我们知道这是StrictHttpFirewall 的一部分,并且不打算使用.setAllowSemicolon(true);

但我们对为什么会发生这种情况感到困惑,因为据我们所知,我们并没有在我们的 URL 上附加任何内容。这是一个错误,似乎也随机弹出,然后随机消失。它也不是通用的 - 用户会开始遇到问题,而其他人还可以,然后他们会没事的。它通常发生在移动数据网络而不是 wifi 上。

建议??其他我们应该看的东西?不要认为它可能与证书有关?我们已经相当宽松地设置了超时,因为我们的应用程序在通常很慢的网络上使用,但如果这个特定错误没有发生,上传通常是成功的。我们正在使用retrofit 2.4okhttp3 3.10

谢谢

【问题讨论】:

  • ApiInterface有哪些方法?
  • 这是发生这种情况的地方:@POST("v1/create-route") Call<LocationCreatedSuccessfully> uploadDestination(@Body CreateRouteRequestV1 result, @Query("userId") int id); 我们还有其他人,坦率地说,这是我们需要从开发中清理的地方。但奇怪的是事情是如何运作的,然后在用户访问应用程序的一天中,它会突然卡在这个问题上。

标签: android spring retrofit2 okhttp


【解决方案1】:

看来只需将 Retrofit 更新为 2.9.0 并将 OkHttp3 更新为 4.9.1 即可解决问题。

【讨论】:

    猜你喜欢
    • 2019-03-05
    • 2022-10-23
    • 2021-08-10
    • 2019-03-27
    • 2019-07-28
    • 1970-01-01
    • 2018-07-05
    • 2018-08-24
    • 2018-11-30
    相关资源
    最近更新 更多