class HttpSocket extends Socket
{
public String getRequestHeaders() throws Exception
{
InputStreamReader isr = new InputStreamReader(getInputStream());
BufferedReader br = new BufferedReader(isr);
String s = "", result = "";
while (!(s = br.readLine()).equals(""))
result = result + s + "
";
return result;
}
}
class HttpServerSocket extends ServerSocket
{
public HttpServerSocket(int port) throws IOException
{
super(port);
}
public Socket accept() throws IOException // 掩盖accept办法
{
Socket s = new HttpSocket();
implAccept(s); // 将accept办法前往的对象范例设为HttpSocket
return s;
}
}
public class CustomAccept
{
public static void main(String[] args) throws Exception
{
HttpServerSocket httpServerSocket = new HttpServerSocket(1234);
HttpSocket httpSocket = (HttpSocket) httpServerSocket.accept();
System.out.println(httpSocket.getRequestHeaders()); // 向把持台输入HTTP哀求头
httpServerSocket.close();
}
}
测试
实行以下命令:
javaserver.CustomAccept
在IE的地点栏中输出以下Url:
http://localhost:1234
CustomAccept在把持台中的运转了局:
GET / HTTP/1.1
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; InfoPath.2)
Host: localhost:1234
Connection: Keep-Alive
下面的运转了局就是IE向服务端收回的HTTP哀求头的内容。这个运转了局会依据客户机设置的分歧而有所差别。