import .*;
import java.io.*;
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 + "\r\n";
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();
}
}
执行如下命令:
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
国内最棒的Google Android技术社区(eoeandroid),欢迎访问!
《银河系列原创教程》发布
《Java Web开发速学宝典》出版,欢迎定购
















