chain.proceed(request) not returning any response.

huangapple go评论67阅读模式
英文:

chain.proceed(request) not returning any response

问题

I have implemented SSL pinning in my react-native app in the android code and i want to get some sort of message/warning when the request is rejected due to invalid SSL hash, so i added an interceptor to get the message from the response.

Here is code of the added interceptor,

OkHttpClient.Builder clientBuilder = OkHttpClientProvider.createClientBuilder();
 return clientBuilder
         .certificatePinner(certificatePinner)
         .addInterceptor(new CustomInterceptor())
         .build();

The problem is when i add an invalid key (for testing purposes), the code below chain.proceed(request) never runs, i am not familiar with OKHTTP or android, but i am guessing it automatically returns a null response in case the request is rejected.

Is there any way to get the response object even if the request is rejected due to invalid SSL hash.

Here is code for my interceptor.

public class CustomInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        System.out.println("My Client::: Intercepted request: " + request.toString());
        Response res = chain.proceed(request);
        System.out.println("My Client::: Intercepted response: " + res.toString());
        return res;
    }
}

In my app Axios is used as a network managing library and i haven't found any library for SSL pinning which works with Axios, that is the reason i had to add that directly at native code. Any other solution regarding this is also welcome.

英文:

I have implemented SSL pinning in my react-native app in the android code and i want to get some sort of message/warning when the request is rejected due to invalid SSL hash, so i added an interceptor to get the message from the response.

Here is code of the added interceptor,

OkHttpClient.Builder clientBuilder = OkHttpClientProvider.createClientBuilder();
         return clientBuilder
                 .certificatePinner(certificatePinner)
                 .addInterceptor(new CustomInterceptor())
                 .build();

The problem is when i add an invalid key (for testing purposes), the code below chain.proceed(request) never runs, i am not familiar with OKHTTP or android, but i am guessing it automatically returns a null response in case the request is rejected.

Is there any way to get the response object even if the request is rejected due to invalid SSL hash.

Here is code for my interceptor.

public class CustomInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        System.out.println("My Client::: Intercepted request: " +request.toString());
        Response res= chain.proceed(request);
        System.out.println("My Client::: Intercepted response: " + res.toString());
        return res;
    }
}

In my app Axios is used as a network managing library and i haven't found any library for SSL pinning which works with Axios, that is the reason i had to add that directly at native code. Any other solution regarding this is also welcome.

答案1

得分: 1

我已经能够通过在 try-catch 块中添加 chain.proceed(request) 并捕获异常来解决这个问题。异常对象包含了请求由于无效的SSL哈希而失败的消息。

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        System.out.println("我的客户端::: 拦截到的请求: " + request.toString());
        Response res = null;
        try {
            res = chain.proceed(request);
        } catch (Exception e) {
            System.out.println(e);
        }
        System.out.println("我的客户端::: 拦截到的响应: " + res.toString());
        return res;
    }
英文:

I have been able to get around this problem by adding the chain.proceed(request) in try-catch block and catching the exception. Exception object contains the message that request failed due to invalid SSL hash.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    System.out.println("My Client::: Intercepted request: " +request.toString());
    Response res= null;
    try{
        res=chain.proceed(request);
    }catch(Exception e){
        System.out.println(e);
    }
    System.out.println("My Client::: Intercepted response: " + res.toString());
    return res;
}

答案2

得分: 0

你考虑过将您的 CustomInterceptor 添加为一个“网络拦截器”吗?这可能有助于从中获取更多信息(尽管不完全确定这是否实际上是您长期所需的)。

return clientBuilder
    .certificatePinner(certificatePinner)
    .addNetworkInterceptor(new CustomInterceptor())
    .build();
英文:

Have you considered adding your CustomInterceptor as a "network interceptor"? That might help to get more information from it (not completely sure that's actually what you need in the long run, though).

return clientBuilder
    .certificatePinner(certificatePinner)
    .addNetworkInterceptor(new CustomInterceptor())
    .build();

huangapple
  • 本文由 发表于 2023年2月13日 23:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定