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:
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();
}
}
Re: BufferedReader.read() blocks,when checking if there's any input for some URLs
Quote:
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.
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
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().
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 :(
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:
Quote:
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>
Re: BufferedReader.read() blocks,when checking if there's any input for some URLs
What you did to get this answer?
thank's
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.
Re: BufferedReader.read() blocks,when checking if there's any input for some URLs