Hello. Look on my code, which can get WWW pages: (for example
http://whatismyip.com/)
InetSocketAddress socketAddress = new InetSocketAddress(new
URL("http://whatismyip.com/").getHost(), 80);
Socket socket = new Socket();
socket.connect(socketAddress);
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
String request = "GET / HTTP/1.1\r\nUser-Agent: Opera/9.20
(Windows NT 5.0; U; pl)\r\nHost: whatismyip.com\r\n\r\n";
writer.write(request);
writer.flush();
StringBuffer content = new StringBuffer();
String line;
BufferedReader reader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
while ((line = reader.readLine ()) != null)
content.append(line).append ("\n");
String response = content.toString();
System.out.println(response);
Unfortunately, there is a error:
java.net.SocketException: Connection reset
in this line:
while ((line = reader.readLine()) != null)
When I download others pages, everything is OK, but this code always
cannot get http://whatismyip.com/ page. Why?
Where is an error?
Thanks for your answer.
Daniel Pitts - 02 May 2007 15:54 GMT
> Hello. Look on my code, which can get WWW pages: (for examplehttp://whatismyip.com/)
>
[quoted text clipped - 29 lines]
>
> Thanks for your answer.
Why not use:
new URL("http://whatismyip.com/").getConnection() or getContent even?
Its easier than dealing with the HTTP protocol yourself.
Roland - 02 May 2007 16:33 GMT
Thanks. When I use:
URLConnection con = url.openConnection();
it is OK, but:
-firstly: why use a socket to get page throws exception? Is it an
error in java platform?
-secondly: how can I abort getting pages? If I want to stop
downloading page, I can use:
socket.close();
but in URLConnection class, there is not close() or abort() method...
Esmond Pitt - 03 May 2007 07:45 GMT
> Thanks. When I use:
> URLConnection con = url.openConnection();
> it is OK, but:
> -firstly: why use a socket to get page throws exception? Is it an
> error in java platform?
No, it's a nasty Web server resetting your connection instead of closing
it properly. It seems that URLConnection copes with that internally,
whereas Socket throws it to you.
> but in URLConnection class, there is not close() or abort() method...
Close the streams.
Gordon Beaton - 03 May 2007 07:36 GMT
> When I download others pages, everything is OK, but this code always
> cannot get http://whatismyip.com/ page. Why?
> Where is an error?
Your code works for me, more or less.
Because your read loop reads to EOF and HTTP 1.1 by default uses
persistent connections, the server doesn't close the connection after
sending the response and the loop blocks until the server eventually
times out the now idle connection. You need to request Connection:
close or obey the Content-Length response header to know when to stop
reading.
Connection reset is typically sent by overloaded Windows servers, and
the host you're having problems with is running Windows. It worked
when I tried it.
However instead of implementing this myself I'd use URLConnection, as
others have suggested.
/gordon
--