这是一个代理服务器程序,目前暂用浏览器打开http://www.sina.com.cn做测试,代码如下:import java.io.*;
import java.util.*;
import java.net.*;public class test extends Thread{
    private Socket clientSocket = null;                         //与客户端通信的socket
    private Socket serverSocket = null;                        //与目标服务器通信的socket
    public static int BUFSIZ=1024;                            //流缓冲区大小
    private InputStream streamFromServer = null;               //从目标服务器的输入流
    private OutputStream streamToServer = null;                //对目标服务器的输出流
    private InputStream streamFromClient = null;               //从客户端的输入流
    private OutputStream streamToClient = null;                //对客户端的输出流    public test(Socket s){
        clientSocket = s;
    }
    public void run(){        
        int headlen = 0;
        try {
            streamFromClient = clientSocket.getInputStream();
            streamToClient = clientSocket.getOutputStream();            //将http头读到header数组
            byte[] header = new byte[2048];
            try{
                headlen = streamFromClient.read(header);                  //如果读头不成功,终止;
            }catch(Exception e2){
                clientSocket.close();
                return;
            }
 
            //建立与目标服务器通信的socket
            serverSocket=new Socket("www.sina.com.cn",80);           //1
            streamFromServer = serverSocket.getInputStream();
            streamToServer = serverSocket.getOutputStream();             //将头写入目标服务器
            streamToServer.write(header);streamToServer.flush();
            //服务器端的信息写入客户端
            int ir = -1;
    byte bytes[]=new byte[BUFSIZ];
            while (true) {
                try {
                    if ((ir=streamFromServer.read(bytes))>0) {
                        streamToClient.write(bytes,0,ir);
                        streamToClient.flush();
                    }
                    else if (ir<0)
                        break;
                }catch (Exception e){                                
                    e.printStackTrace();
                    break;
                }              
            }
                    
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }finally{
                try{
                    if (serverSocket!=null) serverSocket.close();
                    if (clientSocket != null) clientSocket.close();
                }catch (Exception e){
                    e.printStackTrace();
                }    
        }
    }       public static void StartProxy(int port){
        try {
            ServerSocket ss = new ServerSocket(port);                        //创建服务器ServerSockets,等待客户端的请求;
            while(true){
                new test(ss.accept()).start();                          
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    public static void main(String [] args){
        System.out.println("代理服务器在"+808+"端口启动");
        StartProxy(808);
    }
    
}现在的问题是把//1处的www.sina.com.cn换成www.tom.com或者www.sohu.com程序就运行良好,换成www.163.com或者www.sina.com.cn浏览器就不能获取网页,不知道是怎么回事?

解决方案 »

  1.   

    浏览器中显示的是:ERROR
    The requested URL could not be retrieved--------------------------------------------------------------------------------While trying to retrieve the URL: http://www.sina.com.cn/ The following error was encountered: Access Denied. 
    Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect. Your cache administrator is webmaster. --------------------------------------------------------------------------------Generated Wed, 16 Aug 2006 07:11:18 GMT by xa66-56.sina.com.cn (CachePower/1.3.1.dev) 
    如果把网址换成www.tom.com或者www.sohu.com就是正常的
      

  2.   

    对,就是转发浏览器发出的header,但不知为什么新浪和网易就是不行。