调用restAPI时OKHTTP newCall()挂起 [英] OKHTTP newCall() hangs when calling restAPI

查看:638
本文介绍了调用restAPI时OKHTTP newCall()挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用JAVA OKHttp发布到restAPI.

Trying to post to a restAPI using JAVA OKHttp.

我的代码如下:

try {
            loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .build();
            MediaType mediaType = MediaType.parse("app.lication/json");
            RequestBody body = RequestBody.create(mediaType, FileUtils.readFileToString(file));    
            Request request = new Request.Builder()
                    .url(restAPIUrl)
                    .post(body)
                    .addHeader("content-type", "application/json")
                    .addHeader("cache-control", "no-cache")
                    .build();

            Call call = client.newCall(request);
            call.execute();

            handleResponse(file, response.code());

        } catch (Exception e) {

            logger.severe("Http Request failed: " + e.getMessage());

        }

在我的未向restAPI打开防火墙的本地开发盒(centos)上,所有事情的行为均与预期的一样. call.execute()引发异常:无法解析主机:名称或服务不可用.

On my local development box (centos) that doesn't have firewall open to the restAPI, every thing behaves as expected. call.execute() throws an exception: Could not resolve host: Name or service not available.

在生产盒(rhel)上,其中防火墙已打开.对client.newCall(request);的方法调用无限期地挂起. (在调用execute()之前)strace仅显示线程正在等待.

On the production box (rhel), where firewall is open. The method call to client.newCall(request); just hangs indefinitely. (Before calling execute()) strace just shows the thread waiting.

我已验证我可以通过以下命令从命令行访问restAPI:

I've verified i can reach the restAPI from command line with:

curl -X get https://restAPIUrl/index.html

同一罐子在两个服务器上的行为不同.我不知道为什么服务器配置上的差异会影响newCall()(我更希望它在执行时) 是由于环境导致newCall()挂起的原因是什么?任何帮助将不胜感激.

The same jar is behaving differently on the two servers. I can't figure out why differences in server configuration would affect the newCall() (I would more expect it on execute) What could be the cause for newCall() hanging due to environment? Any help would be much appreciated.

谢谢!

更新:

我发现,如果我覆盖defaultSocketFactory,它将超越newCall()方法.

I found that it gets past the newCall() method if I override the defaultSocketFactory.

client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .setSocketFactory(new SocketFactory () {
                       @Override
                        public Socket createSocket(String s int i) thorws IOException, UnknownHostException {
                              return null;
                         }
                         .
                         .
                 })
                .build();

为什么系统不能在rhel env上使用defaultSocketFactory为客户端正确分配套接字? (已禁用SELINUX.)

Why wouldn't the system properly allocate the socket for the client using defaultSocketFactory on the rhel env? (SELINUX disabled.)

更新2:分辨率

最后,此问题是由于在$ JAVA_HOME/lib/security中对"local_policy.jar"没有读取权限引起的. (文件在我的开发环境中不存在)

In the end this issue was caused by no read permissions on 'local_policy.jar' in $JAVA_HOME/lib/security. (File didn't exist in my dev environment)

令人讨厌的部分是OKHttp API吞下了权限异常.

The nasty part was the permissions exception was being swallowed by the OKHttp API.

推荐答案

尝试按照.

只需实现ProviderInstaller.installIfNeeded(或installIfNeededAsync)

这篇关于调用restAPI时OKHTTP newCall()挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆