複製代碼代碼如下:
包cn.liangjintang.httpproxy;
導入java.io..bufferedreader;
導入Java.io.ByTearRayInputStream;
導入java.io.ioexception;
導入java.io.inputstream;
導入java.io.io.inputStreamReader;
導入java.io.outputstream;
導入java.net.serversocket;
導入java.net.socket;
公共類Httpproxy {
靜態長線= 0;
int mytCpport = 8080;
私有式MyServerSocket;
私人線程神話;
公共httpproxy(int端口)拋出ioexception {
mytCpport = port;
myServerSocket = new serversocket(mytCpport);
mythread = new thread(new Runnable(){
public void run(){
嘗試 {
而(true)
新的httpsession(myServerSocket.accept());
} catch(ioexception ioe){
}
}
});
mythread.setdaemon(true);
mythread.start();
}
/**
*停止服務器。
*/
public void stop(){
嘗試 {
myServersocket.Close();
mythread.join();
} catch(ioexception ioe){
} catch(InterruptedException e){
}
}
公共類Httpsession實現可運行{
私人插座mysocket;
公共httpsession(socket s){
mysocket = s;
線程t =新線程(this);
t.setdaemon(true);
t.start();
}
@Override
public void run(){
嘗試 {
++線程;
InputStream is = mySocket.getInputStream();
if(is == null)
返回;
最終int bufsize = 8192;
字節[] buf = new Byte [bufsize];
int splitbyte = 0;
int rlen = 0;
{
int read = is.Read(buf,0,bufsize);
while(讀> 0){
rlen += read;
splitbyte = findheaderend(buf,rlen);
if(splitbyte> 0)
休息;
read = is.Read(buf,rlen,bufsize -rlen);
}
bytearrayInputStream hbis = new bytearrayinputstream(buf,
0,rlen);
bufferedReader hin = new BufferedReader(
新的InputStreamReader(HBIS));
主機host = new host();
{
字符串;
boolean flag = false;
while(((字符串= hin.readline())!= null){
if(string.tolowercase()。startswith(“ host:”)){
host.host =字符串;
flag = true;
}
system.out.println(string);
}
如果(!flag){
mysocket.getOutputstream()。寫(
“錯誤!”。getBytes());
mysocket.close();
返回;
}
}
host.cal();
system.out.println(“地址:” + host.address +“]端口:”
+ host.port +“/n ------------------------/n”);
嘗試 {
管道(buf,rlen,mysocket,mysocket.getInputstream(),
mySocket.getOutputStream(),主機);
} catch(異常E){
system.out.println(“運行異常!”);
e.printstacktrace();
}
}
} catch(異常E){
}
system.out.println(“ threadCount:” + - threadCount);
}
/**
* Finad HTTP標頭
**/
私有int findheaderend(final byte [] buf,int rlen){
int splitbyte = 0;
while(splitbyte + 3 <rlen){
if(buf [splitbyte] =='/r'&& buf [splitbyte + 1] =='/n'
&& buf [splitbyte + 2] =='/r'
&& buf [splitbyte + 3] =='/n')
返回splitbyte + 4;
SplitByte ++;
}
返回0;
}
void pipe(byte []請求,int requestlen,套接字客戶端,
InputStream客戶端,OutputStream Clientos,主機)
拋出例外{
字節字節[] = new Byte [1024 * 32];
套接字插座=新套接字(host.Address,host.port);
socket.setsotimeout(3000);
outputStream OS = socket.getOutputStream();
inputStream is = socket.getInputStream();
嘗試 {
做 {
OS.WRITE(請求,0,請求Len);
int resultlen = 0;
嘗試 {
while((resultlen = is.Read(bytes))!= -1
&&!mysocket.isclosed()&&!socket.isclosed()){
clientos.write(bytes,0,resultlen);
}
} catch(異常E){
system.out.println(“目標套接字異常:”
+ e.tostring());
}
system.out.println(“代理requset-connect損壞,插座:”
+ socket.hashcode());
} while(!mysocket.isclosed()
&&(requestLen = clientis.read(request))!= -1);
} catch(異常E){
system.out.println(“客戶端套接字異常:” + e.ToString());
}
system.out.println(“ end,socket:” + socket.hashcode());
os.close();
is.close();
clientis.close();
clientos.close();
socket.Close();
mysocket.close();
}
//目標主機信息
最終班級主機{
公共字符串地址;
公共INT端口;
公共字符串主機;
public boolean cal(){
如果(主機== null)
返回false;
int start = host.indexof(“:”);
if(start == -1)
返回false;
int next = host.indexof(':',start + 2);
if(next == -1){
端口= 80;
地址= host.substring(start + 2);
} 別的 {
地址= host.substring(start + 2,下一個);
port = integer.valueof(host.substring(next + 1));
}
返回true;
}
}
}
公共靜態void main(string [] args){
嘗試 {
新的httpproxy(8580);
} catch(ioexception ioe){
system.err.println(“無法啟動服務器:/n” + ioe);
system.exit(-1);
}
system.out.println(“ start!”);
嘗試 {
System.in.Read();
} catch(可投擲T){
}
system.out.println(“停止!”);
}
}