我有JAVA的,不过可能可以用转换器转成C#的,大体方法差不多,主要就是在.NET中的流和Socket的类要查一下,改一下,线程要用委托来实现:
import java.net.*;
import java.io.*;public class ProxyServer{
public static void main(String arg[]){
try{
System.out.println("简易HTTP代理服务器 1.0.0.0");
System.out.println("");
System.out.println("开始打开8080端口 ...");
ServerSocket ss=new ServerSocket(8080);
System.out.println("8080端口已正确打开");
System.out.println("");
while(true){
System.out.println("正在等待进行服务 ...");
Socket s=ss.accept();
System.out.println("准备创建服务线程");
Service p=new Service(s);
Thread t=new Thread(p);
t.start();
System.out.println("线程已关闭");
System.out.println("");
}
}catch(Exception e){System.out.println(e);}
}
}class Service implements Runnable{
Socket s;
public Service(Socket s1){
s=s1;
}
public void run(){
String content="";
try{
PrintStream out=new PrintStream(s.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String info=in.readLine();
System.out.println(info);
int sp1=info.indexOf(' ');
int sp2=info.indexOf(' ',sp1+1);
String gotourl=info.substring(sp1,sp2);
System.out.println("正在连接:"+gotourl);
URL con=new URL(gotourl);
InputStream gotoin=con.openStream();
int n=gotoin.available();
byte buf[]=new byte[1024];
out.println("HTTP/1.0 200 OK");
out.println("MIME_version: 1.0");
out.println("Content_Type: text/html");
out.println("Content_Length: "+n);
out.println("");
while((n=gotoin.read(buf))>=0){
out.write(buf,0,n);
}
out.close();
s.close();
in.close();
System.out.println("完成一次服务,准备关闭线程");
}catch(IOException e){
System.out.println(e);
}
}
}