代码之家  ›  专栏  ›  技术社区  ›  parsecer

HttpURLConnection getInputStream太慢

  •  0
  • parsecer  · 技术社区  · 7 年前

    我有一个带有API的站点,用来获取JSON数据。我有一个 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();

    我能做些什么来减少每次请求的时间吗?

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

    编辑 :响应JSON相当大。 connection.setRequestProperty("Accept-Encoding", "gzip"); InputStream input = new GZIPInputStream(connection.getInputStream()); 有帮助,但总共只节省4-5秒。

    0 回复  |  直到 7 年前
        1
  •  1
  •   David Soroko    7 年前

    下面是删除位以允许编译的代码。 Main 做一件 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));
        }
    }