我正在尝试编写一段代码来实现代理服务器。但是,我无法将数据发送回客户端,因为响应如下所示。
响应显示正常响应,稍后出现一些响应,表明 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();
}
}
}
最佳答案
导致 414 错误的原因尚不清楚。如果您没有在代理上使用 Web 浏览器,而是手动发送 GET 请求(或其他一些非 http 请求),则代码中的错误(见下文)可能会导致 GET 请求中包含超过 8196 个字符,这是 Apache 的默认限制。
您应该更改代码,以便使用 String(buffer, offset, length) 构造函数创建字符串,该构造函数使用读取的字节数,而不是完整缓冲区,如果请求和,则完整缓冲区可能包含许多 NULL 值 react 很短。另外,您应该考虑将输出刷新回代理客户端。这是经过这些更改的代码
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[100000];
int bytesRead = inComing.read(buf);
request = new String(buf, 0, bytesRead);
//Create a new socket for connecting to destination server
Socket connSocket = new Socket("localhost", 10003);
PrintWriter pOut = new PrintWriter(connSocket.getOutputStream(), true);
//Reader for response from destination server
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);
pOut.flush();
bytesRead = pIn.read(buf);
if(bytesRead > 0) {
response = new String(buf, 0, bytesRead);
}
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();
}
关于java - Java 中的代理服务器 : Not able to send a response back to the client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18549470/