我正在尝试编写一段代码来实现代理服务器。但是,我无法将数据发送回客户端,因为响应如下所示。
响应显示正常响应,稍后出现一些响应,表示 URI 太大。
HTTP/1.1 414 Request-URI Too Large
Date: Sun, 01 Sep 2013 14:52:20 GMT
Server: Apache/2.2.22 (Ubuntu)
Vary: Accept-Encoding
Content-Length: 325
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>414 Request-URI Too Large</title>
</head><body>
<h1>Request-URI Too Large</h1>
<p>The requested URL's length exceeds the capacity
limit for this server.<br />
</p>
<hr>
<address>Apache/2.2.22 (Ubuntu) Server at 127.0.1.1 Port 80</address>
</body></html>
代码如下。
package ownproxy;
/**
*
* @author mklrjv
*/
import java.net.*;
import java.io.*;
import java.nio.CharBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class InterceptionProxy2 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
int port = 1234;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.out.println("Port Error");
System.exit(-1);
}
while (listening) {
new ProxyThread2(serverSocket.accept()).start();
}
serverSocket.close();
}
}
class ProxyThread2 extends Thread {
private Socket socket = null;
private static final int BUFFER_SIZE = 32768;
public ProxyThread2(Socket socket) {
super("ProxyThread2");
this.socket = socket;
}
public void run() {
PrintWriter outGoing = null;
try {
outGoing = new PrintWriter(socket.getOutputStream(), true);
BufferedReader inComing = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String incomingRequest;
String url = "";
String request = "";
String response = "";
//Take the incoming request
char[] buf = new char[1000000];
inComing.read(buf);
request = new String(buf);
//Create a new socket for connecting to destination server
Socket connSocket = new Socket("localhost", 80);
PrintWriter pOut = new PrintWriter(connSocket.getOutputStream(), true);
BufferedReader pIn = new BufferedReader(new InputStreamReader(connSocket.getInputStream()));
//Put data into the new socket(to the apache server) and receive its output
pOut.print(request);
pIn.read(buf);
response = new String(buf);
System.out.println(response);
//Put data back into the original client socket
outGoing.write(response);
} catch (IOException ex) {
Logger.getLogger(ProxyThread2.class.getName()).log(Level.SEVERE, null, ex);
} finally {
outGoing.close();
}
}
}