Solved

HttpURLConnection

Posted on 2001-08-09
10
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

624 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