私はWebクライアントを書いています。次のコードがあります。
public class Connection extends Thread{
public final static int PORT = 1337;
private ServerSocket svrSocket = null;
private Socket con = null;
public Connection(){
try{
svrSocket = new ServerSocket(PORT);
System.out.println("Conected to: " + PORT);
}catch(IOException ex)
{
System.err.println(ex);
System.out.println("Unable to attach to port");
}
}
public void run(){
while(true)
{
try{
con = svrSocket.accept();//on this part the program stops
System.out.println("Client request accepted");
PrintWriter out = new PrintWriter(con.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
out.println("GET /<index.html> HTTP/1.1");
out.println("***CLOSE***");
System.out.println(in.readLine());
/*
String s;
while((s = in.readLine()) != null)
{
System.out.println(s);
}*/
out.flush();
out.close();
in.close();
con.close();
System.out.println("all closed");
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
}
run メソッドは後で使用します。私が持っているのは、というファイルですindex.html
。このファイルは、Java コードと同じファイルにあります。リクエストでやろうとしているのは、HTML ファイルを送信することです。しかし、このプログラムを Web ブラウザーで実行するとlocalhost:1337
、次のように表示されます。
GET /<index.html> HTTP/1.1
***CLOSE***
これは表示されるべきではありません。の HTML コードの結果のページindex.html
が表示されます。
Index.html コード:
<html>
<head>
<title> </title>
</head>
<body bgcolor = "#ffffcc" text = "#000000">
<h1>Hello</h1>
<p>This is a simple web page</p>
</body>
</html>
この HTML ページをブラウザに表示するにはどうすればよいですか?
ありがとうございました