优化Java客户端HTTPS连接

发布于 2024-11-28 14:16:26 字数 1638 浏览 1 评论 0原文

我正在尝试通过 SSL 与 RESTful API 进行通信。整个客户端应用程序依赖于一个基本的连接方法,看起来像这样,

URL url = null;
       HttpsURLConnection connection = null;
       BufferedReader bufferedReader = null;
       InputStream is = null;

       try {
                url = new URL(TARGET_URL);

                HostnameVerifier hv = new HostnameVerifier() {
                     public boolean verify(String urlHostName, SSLSession session) {
                       return true;
                     }
                };

                HttpsURLConnection.setDefaultHostnameVerifier(hv);
                connection = (HttpsURLConnection) url.openConnection();

                connection.setRequestMethod(requestType);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("Content-Language", "en-US");
                connection.setSSLSocketFactory(sslSocketFactory);

                is = connection.getInputStream();

                bufferedReader = new BufferedReader(new InputStreamReader(is));
                String line;
                StringBuffer lines = new StringBuffer();

                while ((line = bufferedReader.readLine()) != null) {
                    lines.append(line).append(LINE_BREAKER);
                }
                return lines.toString();
       } catch (Exception e) {
           System.out.println("Exception is :"+e.toString());
           return e.toString();
       }
     }

这很好用,但是有更有效的方法吗?我们尝试过 Apache HTTPClient。它有一个非常简单的 API,但是当我们将上述代码与 Apache HTTPClient 和 YourKit 的性能进行比较时,后者创建的对象比第一个更多。我该如何优化这个?

I'm trying to communicate with a RESTful API over SSL. The whole client application relies on a basic connection method which looks something like this

URL url = null;
       HttpsURLConnection connection = null;
       BufferedReader bufferedReader = null;
       InputStream is = null;

       try {
                url = new URL(TARGET_URL);

                HostnameVerifier hv = new HostnameVerifier() {
                     public boolean verify(String urlHostName, SSLSession session) {
                       return true;
                     }
                };

                HttpsURLConnection.setDefaultHostnameVerifier(hv);
                connection = (HttpsURLConnection) url.openConnection();

                connection.setRequestMethod(requestType);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("Content-Language", "en-US");
                connection.setSSLSocketFactory(sslSocketFactory);

                is = connection.getInputStream();

                bufferedReader = new BufferedReader(new InputStreamReader(is));
                String line;
                StringBuffer lines = new StringBuffer();

                while ((line = bufferedReader.readLine()) != null) {
                    lines.append(line).append(LINE_BREAKER);
                }
                return lines.toString();
       } catch (Exception e) {
           System.out.println("Exception is :"+e.toString());
           return e.toString();
       }
     }

This works well, but is there a more efficient way? We've tried Apache HTTPClient. It has an awesomely simple API but when we compared the performance of the above code vs Apache HTTPClient with YourKit, the latter one was creating more objects than the first. How do I optimize this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2024-12-05 14:16:26

我使用过 HTTPClient (但不适用于 HTTPS),但我认为这适用于您的情况。建议为您的服务器创建一个 HTTPClient,并为每个调用创建一个新的 HTTPGet 对象。您需要指定多线程客户端连接管理器,并在初始化 HTTPClient 时指定为每个主机分配适当数量的连接以及最大总连接数。

I've used HTTPClient (but not for HTTPS) but I think this applies to your case. The recommendation is to create a single HTTPClient for your server, and a new HTTPGet object per call. You'll want to specify the multi-threaded client connection manager with an appropriate number of connections to allocate per host, and max total connections when you initialize your HTTPClient.

晨曦慕雪 2024-12-05 14:16:26

您还可以尝试使用 HttpCore 而不是 HttpClientHttpCore 是一组基于 HttpClient 的 HTTP 传输组件。它专门针对低内存占用和性能进行了优化。它缺乏 HttpClient 提供的更高级别的 HTTP 功能(连接管理、cookie 和状态管理、身份验证),但在内存利用率方面应该非常高效。

http://hc.apache.org/httpcomponents-core-ga/examples.html

You could also try to use HttpCore instead of HttpClient. HttpCore is a set of HTTP transport components HttpClient is based upon. It has been specifically optimized for low memory footprint as well as performance. It lacks higher level HTTP functionality provided by HttpClient (connection management, cookie & state management, authentication) but should be quite efficient in terms of memory utilization.

http://hc.apache.org/httpcomponents-core-ga/examples.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文