Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: BufferedReader.read() blocks,when checking if there's any input for some URLs

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default BufferedReader.read() blocks,when checking if there's any input for some URLs

    Hi everyone ,

    i have the following problem :
    I am working on an application that index web pages. When i read the content of some URLs, BufferedReader.read() blocks, e.g. "http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid =29422575". All the alternatives that I've found have not solved this problem. This is my java code:

     public static void main(String[] args) throws Exception {
            // TODO code application logic here
            String _url = "http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=29422575";
            BufferedReader reader = null;
            try {
                URL url = new URL(_url);
                URLConnection urlConnection = url.openConnection();
                reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                    sb.append("\n");
                    System.out.println(line);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
     
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,879
    Thanks
    55
    Thanked 2,346 Times in 2,317 Posts

    Default Re: BufferedReader.read() blocks,when checking if there's any input for some URLs

    BufferedReader.read() blocks
    Where are you using the read() method? I only see the readLine() method which will wait/block until it gets the end of a line character.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BufferedReader.read() blocks,when checking if there's any input for some URLs

    Hi norm,

    I am talking about any read method. This includes readLine() . The problem is that it block with some web page like "http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid =29422575". :s
    thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,879
    Thanks
    55
    Thanked 2,346 Times in 2,317 Posts

    Default Re: BufferedReader.read() blocks,when checking if there's any input for some URLs

    You can test the object to see if there are bytes available to be read before calling read().
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BufferedReader.read() blocks,when checking if there's any input for some URLs

    How can I do that? in fact, there is some data in the beginning, after reading these data, it blocks

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,879
    Thanks
    55
    Thanked 2,346 Times in 2,317 Posts

    Default Re: BufferedReader.read() blocks,when checking if there's any input for some URLs

    Read the API doc for the class and see what methods you can use to determine is there is more data to be read.
    When I try to read from that URL I get:
    HTTP/1.1 400 Bad Request
    Content-Type: text/html; charset=us-ascii
    Server: Microsoft-HTTPAPI/2.0
    Date: Tue, 27 Mar 2012 14:15:32 GMT
    Connection: close
    Content-Length: 311

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
    <HTML><HEAD><TITLE>Bad Request</TITLE>
    <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
    <BODY><h2>Bad Request</h2>
    <hr><p>HTTP Error 400. The request is badly formed.</p>
    </BODY></HTML>
    Last edited by Norm; March 27th, 2012 at 10:16 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BufferedReader.read() blocks,when checking if there's any input for some URLs

    What you did to get this answer?
    thank's

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,879
    Thanks
    55
    Thanked 2,346 Times in 2,317 Posts

    Default Re: BufferedReader.read() blocks,when checking if there's any input for some URLs

    I have a custom program I use to read from URLs.

    When I copied your code and added print outs of some of the fields of the URLConnection object, it executed without blocking.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BufferedReader.read() blocks,when checking if there's any input for some URLs

    ok, thanks

Similar Threads

  1. Replies: 1
    Last Post: February 10th, 2012, 09:49 AM
  2. Replies: 2
    Last Post: November 6th, 2011, 02:33 PM
  3. BufferedReader - Freezes on 2nd read
    By mds1256 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 14th, 2011, 04:26 PM
  4. How to read URLs from a web page
    By abitha in forum Java Theory & Questions
    Replies: 1
    Last Post: September 17th, 2009, 01:36 PM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM

Tags for this Thread