Is an HttpURLConnection's InputStream part of the connection process?

I have a Java program that uses OAuth for communication with a server to retrieve XML data.

It makes use of the Signpost OAuth library to connect with the source, and uses a standard way of reading the InputStream to access the XML that is returned.

Of late, I've noticed the slow time it's taken to retrieve the information and tests have revealed that some requests can take anywhere from 2000 ms up to 10000 ms (if it matters, the source server is in Europe, I am in Australia).

I added a timestamp after the OAuth communication (request.connect()) and again after the reading of the InputStream and here's the output:

Request #1: Communication: [6351ms] Data process: [403ms] Total: [6754ms]
Request #2: Communication: [1ms] Data process: [3121ms] Total: [3122ms]
Request #3: Communication: [1ms] Data process: [1297ms] Total: [1298ms]
Request #4: Communication: [0ms] Data process: [539ms] Total: [539ms]
  • Request #4 is actually Request #2 being run a 2nd time. All requests are made in one run of the program (there's no stopping and starting).

My question: is the InputStream returned to the HttpURLConnection object as part of the connect() method, or is it streamed back as I read from it (as the name suggests) and part of the actual connection process?

Secondary question: With the timing above, is the slow time most likely to be a problem with the server or my method of reading the InputStream?

For reference, here is the code in question:

long startTime = System.currentTimeMillis();
URL url = new URL(urlString);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
request.connect();

long connectionTime = System.currentTimeMillis();

InputStream is = request.getInputStream();
if (is != null) {
    final BufferedReader bufferedreader = new BufferedReader(
              new InputStreamReader(is, "UTF-8"));
    final StringBuffer s2 = new StringBuffer();
    String line;
    line = bufferedreader.readLine();
    if (line != null) {
        s2.append(line);
        while ((line = bufferedreader.readLine()) != null) {
            s2.append('\n');
            s2.append(line);
        }
    }
    bufferedreader.close();
    rv = s2.toString();
}
long finishTime = System.currentTimeMillis();
long timeTaken = finishTime - startTime;
long totalConnectionTime = connectionTime - startTime;
long processDataTime = finishTime - connectionTime;
String info = "Communication: [" + totalConnectionTime + 
                    "ms] Data process: [" + processDataTime + 
                    "ms] Total: [" + timeTaken + "ms]";

Thanks in advance.

Answers


Based on the information provided, here are few observations and suggestions.

  1. To answer your question, the data is streamed back as you read from it. And then you have buffered layers on the top of it. The whole data is not returned and its streamed. I hope I read your question correctly.
  2. The secondary question: The time taken could in both the places viz. the server and in your code as well. Since you are not doing any other processing in your code other than reading the data (except Bufferedreader.close() and s2.toString();) the delay appears to be in server BUT just for being sure, if possible, hit the URL using any browser and see the time taken to fetch the request. (from the code I see that you are just fetching the data from URL and hence should be easy to access the same using browser)
  3. You also have mentioned that you are retrieving XML from the server. I would recommend to use some standard xml parsers (SAX,xstrem etc) which are optimized (hence better performance) for reading xml data from an InputStream.

openConnection() does create the TCP connection, but unless you use a non-default streaming mode no data is sent until you either get the input stream or the reader or the response code. So sending the request is seen as part of getInputStream() in your case.


Need Your Help

# Error: IndentationError: unindent does not match any outer indentation level # On First Line of Code

python indentation maya

I'm getting an error in my python code when trying to import one of my scripts into Maya's memory.

Python static function?

python default-value

Been experiencing this weirdness in my program. Here is a snippet of the part that is giving trouble:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.