Reputation: 1467
I am working on a web server (done), and thought I would make my own little text-based browser, the only problem is that I can't actually get the browser to read the responses. Here is the code:
import java.io.*;
import java.net.*;
class client
{
static Socket socket = null;
static BufferedReader in = null;
static PrintWriter out = null;
public static void main(String args[])
{
int fromServer;
try
{
socket = new Socket("localhost", 8001);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter( new BufferedOutputStream(socket.getOutputStream()));
out.println("GET /Library/WebServer/Documents/index.html.en HTTP/1.0");
out.flush();
while ((fromServer = in.read()) != -1)
{
System.out.write(fromServer);
System.out.flush();
}
}
catch (UnknownHostException e)
{
System.out.println("Unknown host");
}
catch (IOException e)
{
System.out.println("IO error");
}
}
}
Upvotes: 1
Views: 1302
Reputation: 1500365
You haven't completely finished the request. You need two newlines, as otherwise it just looks like you're still writing out the request headers.
Add an extra println
and you may be okay, although as HTTP specifies CRLF for the line ending, I would actually use print
rather than println
, and put \r\n
at the end of each line explicitly.
(I'd also avoid using PrintWriter
, personally - swallowing exceptions is bad...)
Upvotes: 1