我已经使用Java中的套接字实现了代理。 现在我遇到了POST方法的问题,我的意思是,我能够捕获POST请求,但我不知道如何继续捕获此POST的内容(正文,而不是标题)请求,一旦完成我想将内容返回给浏览器(我需要实现这个,因为例如我尝试使用我的用户名和密码登录时它什么都不做)。
我使用以下代码捕获请求:
while ((buffer = in.readLine()) != null){
System.out.println(buffer);
headers[i] = buffer;
i++;
if(buffer.startsWith("GET")){
String[] splitText = buffer.split(" ");
url = splitText[1];
post = false;
}
if(buffer.startsWith("POST")){
String[] splitText = buffer.split(" ");
url = splitText[1];
post = true;
}
if(buffer.startsWith("Host:")){
String[] splitText = buffer.split(" ");
host = splitText[1];
}
if(buffer.isEmpty()){
break;
}
}
我正在尝试使用此代码返回POST请求的正文(显然它不起作用,仅适用于GET请求):
URL u = new URL(url.toString());
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
if(post){
connection.setDoOutput(true);
connection.setRequestMethod("POST");
}
byte[] buf = new byte[8196];
int bytesRead;
InputStream stream;
stream = connection.getInputStream();
while((bytesRead = stream.read(buf)) > 0){
out.write(buf, 0, bytesRead);
out.flush();
}
非常感谢任何帮助。 感谢。
编辑,完整代码:
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedOutputStream out = new BufferedOutputStream(clientSocket.getOutputStream());
String host = null;
String url = null;
int i=0;
String [] headers = new String [100];
String buffer;
boolean post = false;
while ((buffer = in.readLine()) != null) {
System.out.println(buffer);
headers[i]=buffer;
i++;
if(buffer.startsWith("GET"))
{
String[] splitText = buffer.split(" ");
url = splitText[1];
post = false;
}
if(buffer.startsWith("POST"))
{
String[] splitText = buffer.split(" ");
url = splitText[1];
post = true;
}
if(buffer.startsWith("PUT"))
{
String[] splitText = buffer.split(" ");
url = splitText[1];
post = true;
}
if(buffer.startsWith("Host:"))
{
String[] splitText = buffer.split(" ");
host = splitText[1];
}
if (buffer.isEmpty()) break;
}
if (!checkURL(host) && !checkWORD(url)){
URL u = new URL(url.toString());
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
if (post){
connection.setDoOutput(true);
connection.setRequestMethod("POST");
}
for (int x=1;x<i-1;x++){
if (!headers[x].contains("Accept-Encoding:")){
connection.setRequestProperty(headers[x].substring(0, headers[x].indexOf(":")).toString() , headers[x].replace(headers[x].substring(0, headers[x].indexOf(":") +2), "").toString());
}
}
boolean redirect = false;
int status = ((HttpURLConnection) connection).getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
if (redirect) {
String Url = connection.getHeaderField("Location");
URL urlloc = new URL(Url.toString());
connection = (HttpURLConnection) urlloc.openConnection();
if (checkWORD(Url)){
PrintWriter outt = new PrintWriter(clientSocket.getOutputStream());
outt.write("HTTP/1.0 200 OK\r\n");
outt.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
outt.write("Server: Apache/0.8.4\r\n");
outt.write("Content-Type: text/html\r\n");
outt.write("Content-Length: 57\r\n");
outt.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
outt.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
outt.write("\r\n");
outt.flush();
outt.close();
}
for (int x=1;x<i-1;x++){
if (!headers[x].contains("Accept-Encoding:")){
connection.setRequestProperty(headers[x].substring(0, headers[x].indexOf(":")).toString() , headers[x].replace(headers[x].substring(0, headers[x].indexOf(":") +2), "").toString());
}
}
}
byte[] buf = new byte[8196];
int bytesRead;
InputStream stream;
stream = connection.getInputStream();
while ((bytesRead = stream.read(buf)) > 0) {
out.write(buf, 0, bytesRead);
out.flush();
}
}else{
PrintWriter outt = new PrintWriter(clientSocket.getOutputStream());
outt.write("HTTP/1.0 200 OK\r\n");
outt.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
outt.write("Server: Apache/0.8.4\r\n");
outt.write("Content-Type: text/html\r\n");
outt.write("Content-Length: 57\r\n");
outt.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
outt.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
outt.write("\r\n");
outt.flush();
outt.close();
}
out.close();
in.close();
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}