I'm trying to create a socket that connects to a server and sends an http request. The request gets sent, but I get absolutely no response. I flush the connection, but it does absolutely nothing. I created a class that tries to read the default file on my local Tomcat server, but nothing. Can anyone figure out what I've done wrong? The end result of the code I've posted below is that it sends the request, waits ten seconds, then prints out the failure message. TIA, Alex
You have a couple of things going on here. First, you may want to swing by the HTTP Specification and check out what the format of a GET statement is. In particular, the placement of carrage-returns and line-feeds. Following the protocol is the first step to success. You will need to flush the output stream, so un-comment that call. Finally, calling ready() on a BufferedReader doesn't tell you if the Reader is "ready" to provide output. The documentation says:
public boolean ready() throws IOException Returns: True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
Evaluating if a read will block or not is not particularly interesting. Instead, invoke readLine() on the BufferedReader until it returns null (indicating end-of-stream, see above doc for details). [ April 26, 2004: Message edited by: Joe Ess ]
I uncommented the flush and it worked, thanks. I don't get it though, since I tried that before I posted and it didn't, maybe I had something else going on also. Now that this works, I'll try plugging it into my real application and see if I can get that to work. Thanks, Alex