0

This is the code I'm using to get response text.

   private static String request(String urlstr){

    // create connection
    try {
        URL url = new URL(urlstr);
        URLConnection conn = url.openConnection();
        StringBuilder response = new StringBuilder();
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", USER_AGENT);

        // read response
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            response.append(line);
        }
        in.close();

        return response.toString();
    } catch (Exception e){
        return null;
    }
}

The problem is that when querying the very same request (simple get request, response is json) with my chrome browser I get the response almost 1 second faster than with this code in my application.

I wonder if theres anything I'm doing wrong in my code? Or it is chrome handling that request faster somehow?

Maybe there are some techniques to make this process faster?

Thanks

7
  • Perhaps Chrome already has an open connection to the site in question? You should use Wireshark or something similar in conjunction with logging in your app to see what is taking the time. Commented May 14, 2014 at 9:10
  • I see nothing wrong with your code. Maybe Chrome returns the result from the cache? Also I assume you excluded the JVM startup time from your measurements. Commented May 14, 2014 at 9:12
  • Sure i've excluded all non related, I just dont think it is taking whole second to open a connection :( Commented May 14, 2014 at 9:25
  • Is chrome configured to go through a proxy? BTW: why do you tag httpclient when you're not even using it? Did you try it? Perhaps that makes a difference, HttpClient is vastly superior. Commented May 14, 2014 at 9:36
  • Is your URL maybe HTTPS? Certificate validation and revocation list checks can take some time. Commented May 14, 2014 at 9:39

1 Answer 1

1

You seem to read the response line by line, but in the end you append every line to one single response, so it is not really required to read the response line by line. You can also do

  char[] cbuf = new char[1024];
  int len;
  while ((len = in.read(cbuf)) != -1)
      response.append(cbuf, 0, len);

Like this the response can be read in much larger chunks and you don't have the overhead of the readLine() method that has to look for newline characters in the input and split the content into lines.

You could also do a

  new StringBuilder(connection.getContentLength());

to avoid that the StringBuilder has to increase it's capacity every time new content is appended. The StringBuilder is using a char[] internally and every time the array is not big enough for the new content it has to be copied to a new array with a larger size.

Sign up to request clarification or add additional context in comments.

1 Comment

I've just tried your suggestion, but it improved only ~ (30ms - 45ms). Anyway thanks this is better approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.