Solved

HttpURLConnection

Posted on 2001-08-09
10
193 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
Comment Utility
0
 

Expert Comment

by:raghu_k_n
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:mbutu
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now