【问题标题】:curl command line vs HttpURLConnection API service call response time differencescurl 命令行 vs HttpURLConnection API 服务调用响应时间差异
【发布时间】:2016-03-22 10:43:44
【问题描述】:

我正在使用使用 HttpURLConnection 的 Web 服务,它需要 60 秒才能返回响应,但是当我使用 CURL(命令行)进行具有相同参数的相同操作时,只需要 20 - 25 秒即可返回响应。

通过 HttpURLConnection 调用 API 服务时可能出现什么问题,因为返回响应需要更长的时间。

HttpURLConnection API 调用代码:

`

        url = new URL(this._serviceURL);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        connection.setRequestProperty("Accept", "application/xml;");
        connection.setRequestProperty("SOAPAction", "http://www.xxtest.com/Request");
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // Send request
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(xmlRequest);
        wr.flush();
        wr.close();

        // Get Response
        responseCode = connection.getResponseCode();

        String xmlResponse = "";
        if (responseCode == HttpURLConnection.HTTP_OK) { // success

            is = connection.getInputStream();
            xmlResponse = IOUtils.toString(is);
            // Decode base64 and Decompress
            final GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(Base64.decodeBase64(xmlResponse.getBytes())));
            xmlResponse = IOUtils.toString(gzipInputStream);
        }`

CURL 命令:

curl -XPOST -H "Content-type: text/xml" -H "SOAPAction: http://www.xxtest.com/Request" -H "Accept: application/xml;" -d @request_soap.xml 'http://www.xxtest.com/xmlservices.asmx' > response.xml

更新: 上面提到的 HttpURLConnection API 调用 java 代码 - 当从 Web 应用程序(Tomcat)执行时,返回响应需要更长的时间(60 秒),但是当我在同一台服务器上运行与独立 java 程序相同的 java 代码时,它会返回响应在 20 秒内。完全相同的代码。现在,我不明白为什么从 Web 应用程序执行相同的代码需要更长的时间。

【问题讨论】:

  • 您能否也展示一下您是如何阅读回复的?也许那里正在发生一些事情。
  • @adhesivee InputStream 到 xmlResponse 字符串转换几乎不需要 1 秒到 2 秒。

标签: java performance web-services curl httpconnection


【解决方案1】:

我在 HttpURLConnection API 调用代码(在我的问题中提到)的 openConnection 方法中添加了 Proxy.NO_PROXY 并解决了延迟响应问题。我使用的服务有代理设置,因此我面临延迟响应问题。 请检查下面提到的代码 sn-p。

connection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);

【讨论】:

    【解决方案2】:

    性能问题可能就在此时

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.writeBytes(xmlRequest);
    wr.close();
    

    我猜跟随会获得更好的性能。

    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
    out.write(xmlRequest);
    out.close();
    

    【讨论】:

    • 我在那里使用基于 Axis 客户端 Stub 的 API 服务调用,我也面临响应延迟问题,因此我进行了更改并实现了 HttpURLConnection 以使用 API 服务。我担心如果 CURL 可以在 20 秒内收到响应,那么为什么基于存根的 API 服务调用和 HttpURLConnection API 服务调用需要大约 60 秒。如果存在网络问题,那么 CURL 也应该需要时间,但这不会发生。我会执行您的建议并在此处更新。
    • @Santosh 也许您应该首先尝试隔离发生延迟的部分。我的回答或多或少是有根据的猜测。而且我通常不会认为这会慢三倍。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2010-09-10
    • 2014-12-11
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多