当前位置: 技术问答>java相关
一段应该没有错的代码
来源: 互联网 发布时间:2015-01-09
本文导语: 是个proxy反正我调不通,谁再试试。通了请告诉我。只能加20分,先到先得了:) import java.net.*; import java.lang.*; import java.io.*; public class WebProxy extends Thread { private Socket skClient, skServer; private BufferedR...
是个proxy反正我调不通,谁再试试。通了请告诉我。只能加20分,先到先得了:)
import java.net.*;
import java.lang.*;
import java.io.*;
public class WebProxy extends Thread {
private Socket skClient, skServer;
private BufferedReader brClient, brServer;
private PrintWriter pwServer, pwClient;
private static final int iDATABUFFERSIZE = 25;
private String sLocalIP;
private static String sServer = "Server> ";
private static String sClient = "Client> ";
private static String sProxy = "Proxy > ";
private static String sSpace = " ";
private static final String HTTP_VERSION = "HTTP/1.0";
private static final String CRLF = "rn";
public WebProxy (Socket skClient) {
this.skClient = skClient;
}
public void run() {
String sFromClient, sFromServer;
try {
brClient = new BufferedReader(new InputStreamReader(skClient.getInputStream()));
pwClient = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(skClient.getOutputStream())));
sFromClient = brClient.readLine();
if (sFromClient == null) {
pwClient.close();
return;
}
//System.out.println(sFromClient);
int i1 = sFromClient.indexOf(' ');
int i2 = sFromClient.lastIndexOf(' ');
if (i1 == -1 || i2 == -1) {
//simple validation af input
//sFromServer = "531 Incorrect usage - closing connection.";
//System.out.println(sProxy + sFromServer);
//pwClient.print(sFromServer + "rn");
//pwClient.flush();
//skControlClient.close();
//return;
}
URL url = new URL(sFromClient.substring(i1 + 1, i2));
if (!url.getProtocol().equals("http")) throw new IOException();
String httpVersion, method;
int serverPort = url.getPort() == -1 ? 80 : url.getPort();
method = sFromClient.substring(0, i1);
httpVersion = sFromClient.substring(i2 + 1);
skServer = new Socket(url.getHost(), serverPort);
brServer = new BufferedReader(new InputStreamReader(skServer.getInputStream()));
pwServer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(skServer.getOutputStream())));
pwServer.print(method + " " + url.getFile() + " " + HTTP_VERSION + CRLF);
//System.out.println(method + " " + url.getFile() + " " + HTTP_VERSION);
//read/send headers from client
int contentLength = 0;
while (!(sFromClient = brClient.readLine()).equals("")) {
if (sFromClient.startsWith("Connection: ")) {
} else if (sFromClient.startsWith("Proxy-")) {
} else {
//System.out.println(sFromClient);
pwServer.print(sFromClient + CRLF);
if (sFromClient.toLowerCase().startsWith("content-length: ")) {
contentLength = Integer.parseInt(sFromClient.substring(16));
}
}
}
pwServer.print("Connection: close" + CRLF);
pwServer.print(CRLF);
//System.out.print("Connection: close" + CRLF + CRLF);
pwServer.flush();
int noBytes = 0;
int i = 0;
char bRead[] = new char[iDATABUFFERSIZE];
while (noBytes
import java.net.*;
import java.lang.*;
import java.io.*;
public class WebProxy extends Thread {
private Socket skClient, skServer;
private BufferedReader brClient, brServer;
private PrintWriter pwServer, pwClient;
private static final int iDATABUFFERSIZE = 25;
private String sLocalIP;
private static String sServer = "Server> ";
private static String sClient = "Client> ";
private static String sProxy = "Proxy > ";
private static String sSpace = " ";
private static final String HTTP_VERSION = "HTTP/1.0";
private static final String CRLF = "rn";
public WebProxy (Socket skClient) {
this.skClient = skClient;
}
public void run() {
String sFromClient, sFromServer;
try {
brClient = new BufferedReader(new InputStreamReader(skClient.getInputStream()));
pwClient = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(skClient.getOutputStream())));
sFromClient = brClient.readLine();
if (sFromClient == null) {
pwClient.close();
return;
}
//System.out.println(sFromClient);
int i1 = sFromClient.indexOf(' ');
int i2 = sFromClient.lastIndexOf(' ');
if (i1 == -1 || i2 == -1) {
//simple validation af input
//sFromServer = "531 Incorrect usage - closing connection.";
//System.out.println(sProxy + sFromServer);
//pwClient.print(sFromServer + "rn");
//pwClient.flush();
//skControlClient.close();
//return;
}
URL url = new URL(sFromClient.substring(i1 + 1, i2));
if (!url.getProtocol().equals("http")) throw new IOException();
String httpVersion, method;
int serverPort = url.getPort() == -1 ? 80 : url.getPort();
method = sFromClient.substring(0, i1);
httpVersion = sFromClient.substring(i2 + 1);
skServer = new Socket(url.getHost(), serverPort);
brServer = new BufferedReader(new InputStreamReader(skServer.getInputStream()));
pwServer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(skServer.getOutputStream())));
pwServer.print(method + " " + url.getFile() + " " + HTTP_VERSION + CRLF);
//System.out.println(method + " " + url.getFile() + " " + HTTP_VERSION);
//read/send headers from client
int contentLength = 0;
while (!(sFromClient = brClient.readLine()).equals("")) {
if (sFromClient.startsWith("Connection: ")) {
} else if (sFromClient.startsWith("Proxy-")) {
} else {
//System.out.println(sFromClient);
pwServer.print(sFromClient + CRLF);
if (sFromClient.toLowerCase().startsWith("content-length: ")) {
contentLength = Integer.parseInt(sFromClient.substring(16));
}
}
}
pwServer.print("Connection: close" + CRLF);
pwServer.print(CRLF);
//System.out.print("Connection: close" + CRLF + CRLF);
pwServer.flush();
int noBytes = 0;
int i = 0;
char bRead[] = new char[iDATABUFFERSIZE];
while (noBytes