gpt4 book ai didi

java - curl 命令行 vs HttpURLConnection API 服务调用响应时间差异

转载 作者:行者123 更新时间:2023-11-30 08:43:22 31 4
gpt4 key购买 nike

我正在使用使用 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 应用程序执行时需要更长的时间。

最佳答案

性能问题可能就在此时

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

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

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

关于java - curl 命令行 vs HttpURLConnection API 服务调用响应时间差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34309611/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com