What is Proxy Web Server
- 1.Receive HTTP request from A
- 2.Transmit request to real server
- 3.Receive response from real server
- 4.Transmit response to A
- a)Use HTTP request
- b)Use Socket connect and send and receive
Core(重点)
- Socket
- HTTP protocol(HTTP协议)
Environment
- OS: windows 7 旗舰版
- editor: Eclipse
- jdk8
Build
Set port and IP
//Port of Proxy Server
int InPort = 8081;
//Port of Real Server
int OuPort = 80;
//IP of real server
String OuHost = "10.0.0.52";
Build ServerSocket
try {
//Build serverSocket with request port
serverSocket = new ServerSocket(InPort);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
If building errors,the program would be stop and print error message
Receive request
InSocket = serverSocket.accept();
Connect with real Server
OuSocket = new Socket(OuHost, OuPort);
Read request and save in reader
input = InSocket.getInputStream();
Reader reader = new InputStreamReader(input);
Transmit request to real Server
output = OuSocket.getOutputStream();
char chars