http代理程序——JAVA

来源:互联网 发布:mysql 被入侵 编辑:程序博客网 时间:2024/06/18 06:07

/*
对post方法后的数据还无法处理
*/
import java.io.*;
import java.net.*;
class http
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(8000);//监听8000端口
while(true)
{
Socket s=ss.accept();//取得连接
getmes g=new getmes(s);//产生线程
}//while
}//main
}//http

class getmes extends Thread
{
Socket ss;
String webip;//web服务器域名
String sendstr=new String("");//要发送给web的信息
String gp;//所用方法POST还是GET 
getmes(Socket ns)
{ss=ns;start();}
public void run()
{
System.out.println("接收到用户信息:");
try{
DataInputStream in=new DataInputStream(ss.getInputStream());
String head=new String();
head=in.readLine();//读取第一行信息
if(head.indexOf("GET ")!=-1) gp=new String("GET ");//如果是GET
if(head.indexOf("POST ")!=-1)gp=new String("POST ");//如果是POST
int a=head.indexOf("http://");
String dns=head.substring(a+new String("http://").length()+1);
a=dns.indexOf(" HTTP/1.0");
String tem=new String(dns.getBytes(),0,a);
a=tem.indexOf("/");
tem=tem.substring(a);
head=gp+tem+" HTTP/1.0";
while(head.length()!=0)
{
sendstr+=head+"/r/n";
if(head.indexOf("Host")!=-1)
{
webip=head.substring(head.indexOf("Host: ")+new String("Host: ").length());//取得web服务器名
}
head=in.readLine();
}//while
sendstr+="/r/n/r/n";
//以上是得到HTTP请求信息
FileOutputStream blog=new FileOutputStream("blog//usersendstr.txt",true);//记录到日志文件
blog.write(sendstr.getBytes());
blog.flush();
blog.close();
System.out.print(sendstr);
Socket sweb=new Socket(webip,80);//与WEB服务器连接
DataOutputStream out=new DataOutputStream(sweb.getOutputStream());
out.write(sendstr.getBytes());//发出请求
out.flush();
System.out.println("以发出用户信息");
rstr r=new rstr(ss,sweb);//产生接收信息线程
}//try
catch(Exception e){System.out.println(e);}
}//run
}//getmes

class rstr extends Thread
{
Socket s;
Socket web;
rstr(Socket ns,Socket nweb)
{
s=ns;web=nweb;start();
}
public void run()
{
try{
DataInputStream in=new DataInputStream(web.getInputStream());//读取web服务器返回信息
DataOutputStream out=new DataOutputStream(s.getOutputStream());//向用户写web返回的信息
FileOutputStream blog=new FileOutputStream("blog//getwebstr.txt",true);//记录WEB返回的信息到日志程序中
System.out.println("开始接收web信息");
System.out.println("开始发送web信息");
byte[] b=new byte[3036];
int num;
num=in.read(b);
while(num!=-1)
{
blog.write(b,0,num);
out.write(b,0,num);
num=in.read(b);
}//while
blog.close();
}catch(Exception e){System.out.println(e);}
}//run
}//rstr