Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HttpURLConnection

Posted on 2001-08-09
10
Medium Priority
?
201 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1200 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

670 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