我正在使用网络服务器(完成),并认为我会制作自己的基于文本的小型浏览器,唯一的问题是我实际上无法让浏览器读取响应。这是代码:
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");
}
}
}