HttpURLConnection getInputStream太慢

时间:2019-02-24 18:51:30

标签: java httpurlconnection

我有一个带有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中获取一个参数,并将其传递给新请求中的链接)。

1 个答案:

答案 0 :(得分:1)

下面是您的代码,其中的位被删除以允许编译。 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客户端。