gpt4 book ai didi

java - HttpURLConnection getInputStream 太慢

转载 作者:行者123 更新时间:2023-11-30 05:45:48 25 4
gpt4 key购买 nike

我有一个带有 API 的网站,我用它来获取 JSON 数据。我有一个 ConnectClass 类,每次发送请求时都会创建一个实例。当创建 ConnectClass 实例时,会创建一个新的 HttpURLConnection 对象、.setup().connect()编辑:

class ConnectClass  {
private HttpURLConnection connection;

private String link;
private REQUEST_TYPE requestType;
private URL url;
private AccessToken accessToken;

public String send() throws Exception {
connection.connect();
System.out.println("start get input stream"); //from this
InputStream input = connection.getInputStream();

System.out.println("end get input stream"); //to this takes too long

System.out.println("Start scanner");
String inputString = new Scanner(input, "UTF-8").useDelimiter("\\Z").next();
System.out.println("End scanner");
input.close();

return inputString; //returns response JSON string
}

public ConnectClass(String link, AccessToken accessToken, REQUEST_TYPE requestType) throws Exception {
this.link = link;
this.accessToken = accessToken;
this.requestType = requestType;

this.url = new URL(link);
connection = (HttpURLConnection) url.openConnection();
setup();
}

private void setup() throws Exception {
//connection.setDoOutput(true);
connection.setConnectTimeout(100); //doesn't really change things
//connection.setChunkedStreamingMode(1024);
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("User-Agent", "some description v2.0");

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

connection.setRequestProperty("Authorization", "Bearer " + accessToken.ACCESS_TOKEN_LONG);
connection.setRequestMethod("GET");

connection.setInstanceFollowRedirects(false);
}
}

但是,我需要循环发送 10 个这样的请求。每个请求大约需要 1.3 秒,而所有 10 个请求加起来的时间不应超过 1-2 秒。我发现大部分时间都花在获取输入流并处理它:InputStream input = connection.getInputStream(); - 大约需要 0.6-1 秒,String inputString = new Scanner( input, "UTF-8").useDelimiter("\\Z").next(); 大约需要 0.1-0.2 秒。

我可以做些什么来减少每个请求的时间吗?

我尝试将连接超时设置为低至 100,但没有明显效果。

编辑:响应 JSON 相当大。 connection.setRequestProperty("Accept-Encoding", "gzip"); 然后使用 InputStream input = new GZIPInputStream(connection.getInputStream()); 有帮助,但仅此而已总共节省了大约 4-5 秒。

我无法使用并发请求 - 每个新请求都依赖于前一个请求(从之前的输入 JSON 中获取参数并将其传递到链接中的新请求中)。

最佳答案

下面是您的代码,删除了一些位以允许编译。 Main 在 Google 上执行 GET。在我的机器上,send 大约需要 300 毫秒。去完成。如果您遇到类似的情况,我建议您检查您的帐户是否已正确设置并且没有受到限制

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

class ConnectClass {
private HttpURLConnection connection;

private String link;
private URL url;

public String send() throws Exception {
connection.connect();
System.out.println("start get input stream"); //from this
InputStream input = connection.getInputStream();

System.out.println("end get input stream"); //to this takes too long

System.out.println("Start scanner");
String inputString = new Scanner(input, "UTF-8").useDelimiter("\\Z").next();
System.out.println("End scanner");
input.close();


// System.out.println(inputString);
return inputString; //returns response JSON string
}

public ConnectClass(String link) throws Exception {
this.link = link;

this.url = new URL(link);
connection = (HttpURLConnection) url.openConnection();
setup();
}

private void setup() throws Exception {
connection.setDoOutput(true);
connection.setConnectTimeout(100); //doesn't really change things
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("User-Agent", "some description v2.0");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

connection.setRequestMethod("GET");
connection.setInstanceFollowRedirects(false);
}


public static void main(String[] args) throws Exception {

ConnectClass cc = new ConnectClass("https://www.google.com");

long start = System.currentTimeMillis();
cc.send();
System.out.println("Done in " + (System.currentTimeMillis() - start));
}
}

另一种可能性是您收到了大量回复。如果是这样,您可能需要使用支持压缩的 HTTP 客户端。

关于java - HttpURLConnection getInputStream 太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54855326/

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