Solved

Writing a HTTPClient

Posted on 2000-03-13
5
193 Views
Last Modified: 2008-03-17
Hello ,

I try to write a bot which fetches a Website by a given URL using the URL and HTTPCon. classes.
My Problem is, i am using s.th. like << while ( (byteArr = f.read()) != null) >> to check if the read packet is null or not for the while loop condition.
But with this approach I dont get the full file cause sometimes the connection is stalled and I only get half of the site. Is there a possibility which is compactible with HTTP 1.0 and 1.1 to check wether the file is full read or not

Thanx a lot for any advices and hints ...

Binh  
0
Comment
Question by:kaber
5 Comments
 

Expert Comment

by:kbeier
ID: 2614465
Hi kaber,

What about using a BufferedReader:

BufferedReader in = new BufferedReader(new InputStreamReader(MyHTTPUrlConnection.getInputStream()));

and then using:
String inputLine;
while ((inputLine = in.readLine())
{
    //do your stuff here
}

Just a suggestion.  Hope it helps.

:-)
0
 
LVL 4

Expert Comment

by:jerch
ID: 2614483
Yes, you can use the content-length header (use getContentLength() of HttpURLConnection).  But the problem not all web sites provide this information.  If the content-length is available, you can compare the size of the actual file to the content-length header.  So you would be able to determine if you were able to download the complete file.

Hope this helped...

Jerson
0
 

Expert Comment

by:zevious
ID: 2614851
Use Socket.setSoTimeout();

This might solve it...
0
 

Author Comment

by:kaber
ID: 2615639
Hi ,

kbeier your comment helps me out to solve the prob. Thanx a lot , I would give you the points, but how can I do that ?
0
 

Accepted Solution

by:
kbeier earned 100 total points
ID: 2616012
Hi kaber,

I will post my comment as an answer for you (hopefully this will let you allocate the points ;-)).  My comment was:

What about using a BufferedReader:

BufferedReader in = new BufferedReader(new InputStreamReader(MyHTTPUrlConnection.getInputStream()));

and then using:
String inputLine;
while ((inputLine = in.readLine())
{
    //do your stuff here
}

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

792 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