Solved

HttpURLConnection

Posted on 2001-08-09
10
195 Views
Last Modified: 2006-11-17
How do i get the total size of the content received with a HttpURLConnection, including the headers?

And how can i get the total amount of bytes read from the stream, including the headers, without counting them myself?


HTTP/1.0 GET ............
.
headername:blabla
.
.
<html>
</html>



0
Comment
Question by:mbutu
10 Comments
 
LVL 10

Expert Comment

by:rajesh_bala
ID: 6367728
0
 

Expert Comment

by:raghu_k_n
ID: 6367743
Hi there,
      There are API's on the InputStream to get the available length. U get the InputStream from the URLConnection by saying URLConnection.getInputStream

-HTHU
Raghu
0
 

Author Comment

by:mbutu
ID: 6368426
I need to count all bytes. That includes \n\r etc.
Using getHeaderField and adding all field lengths to content length will not include \n\r. getContentLength() in HttpURLConnection only returns the content length. Not the headers + content + everything else.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:mbutu
ID: 6368476
I need to count all bytes. That includes \n\r etc.
Using getHeaderField and adding all field lengths to content length will not include \n\r. getContentLength() in HttpURLConnection only returns the content length. Not the headers + content + everything else.
0
 
LVL 1

Expert Comment

by:lawrie
ID: 6368916
This may be what you mean by counting them yourself, but generally I read from a connection using the following, because it handles all types of connections and spped of connections. getContentLength() isn't accurate, and may even return -1 if the server isn't nice to you.

--------------------------------
InputStream stream = connection.getInputStream();

int readTotal = 0;          
int readAmount = 0;
int readLength = 1000;
byte [] characters = new byte[readLength];

// Read the stream data
while (readAmount >= 0)
{
    readAmount = stream.read(characters, 0, readLength);
    if (readAmount > 0)
    {
        output.write(characters, 0, readAmount);
        readTotal += readAmount;
    }
}
0
 

Author Comment

by:mbutu
ID: 6368943
I need to count all bytes. That includes \n\r etc.
Using getHeaderField and adding all field lengths to content length will not include \n\r. getContentLength() in HttpURLConnection only returns the content length. Not the headers + content + everything else.
0
 

Author Comment

by:mbutu
ID: 6371276
Yes lawrie, it is the same thing i do to count bytes. But why is there no getTotalBytesRead() in InputStream. It would save a lot of trouble. And it means that i can't use HttpURLConnection.

Or am i to lazy?
0
 
LVL 1

Accepted Solution

by:
lawrie earned 300 total points
ID: 6373603
I suspect that in most uses getContentLength is sufficient for people, since they just want the size of the thing being retrieved, not the actual amount retrieved.

There may be another way I don't know about, but I guess it is up to you whether it is worth the effort hunting for it.

It just means you have to write your own function and / or class to do what you want .. you will only have to do it once :)
0
 

Expert Comment

by:CleanupPing
ID: 9061898
mbutu:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 9329979
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for lawrie

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question