Solved

HttpURLConnection

Posted on 2001-08-09
10
198 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 264
JAVA API design with micro service cloud in mind 1 110
Tagging and Merging on Branch 1 68
Coding for the first time 9 100
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…
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 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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

734 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