0

我有服务器作为代理(中介)拒绝某些网站。我设置了运行我的程序的机器的 IP 地址和端口号(例如 127.0.0.1:8080)。我打开了一个 TCP 套接字并监听来自浏览器的请求。当我尝试进入被禁止的网站时,我的浏览器会显示错误页面。但是如果我想进入的网页没有被禁止,我应该在我的浏览器上显示它。我无法完成最后一部分。我无法访问不受限制的网站。我想我应该使用另一个带有端口 80 的套接字,但我不确定。

这就是我检查网站是否被禁止的方式。如果被禁止,则返回错误页面。否则它应该显示网页。

import java.io.*;

import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;

public class ClientHandler2 extends Thread {

private Socket clientSocket = null;
private DataOutputStream out = null;
private BufferedReader in = null;
//  private Socket mySocket = null;


public ClientHandler2(Socket clientSocket) {
    this.clientSocket = clientSocket;
}

public void run() {


    try {
        //create input-output streams
    //  in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        out = new DataOutputStream(clientSocket.getOutputStream());

        ReqHdr req = new ReqHdr();
        RepHdr rep = new RepHdr();
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));


        if(req.parse(clientSocket.getInputStream())){
            MyReader myreader = new MyReader();
            if(myreader.IsBanned(req.url ) == true ){

                InetAddress addr = InetAddress.getLocalHost();
                String IP = addr.getHostAddress();
                Date date = new Date();
                String datestr = date.toString();
                 try{
                      // Create file 
                      FileWriter fstream = new FileWriter("log.txt",true);
                      BufferedWriter log = new BufferedWriter(fstream);
                      log.append("Thread with id: " + this.getId() + " Banned URL:  " + req.url + " IP: " + IP + " Access Time/Date: " + datestr + "\n");
                      //Close the output stream
                      log.close();
                      }catch (Exception e){//Catch exception if any
                      System.err.println("Error: " + e.getMessage());
                      }

                out.writeUTF((rep.formForbidden()));

            }
            else{
                System.out.println("Thread with id " + this.getId() + " URL: " + req.url);


            }
        }
        else{
            System.out.println("error");
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

每当我尝试进入网站时,我都会在她那里创建一个新线程。

import java.io.IOException;
import java.net.*;

public class WebPolice {


public static void main(String args[])
{
    //create server socket
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(8080);
        System.out.print("WebPolice listening clients over port 8080...");
    } catch (IOException e1) {
        e1.printStackTrace();
    }


    //at each iteration of the loop, accept new incoming connection and serve the client
    while (true)
    {
        //create client socket
        Socket clientSocket = null;
        try {

            //accept incoming connection request
            clientSocket = serverSocket.accept();
            System.out.println("New client connected!");
        } catch (IOException e) {
            e.printStackTrace();
        }

        //create new thread object for the new coming client
        //note that ClientHandler extends Thread
        ClientHandler2 clientHandler = new ClientHandler2(clientSocket);

        //new power for next client

        //run the Thread which deals with the new coming client
        clientHandler.start();


    }
}
}
4

1 回答 1

0
 System.out.println("Thread with id " + this.getId() + " URL: " + req.url);

在上面的块中,使用 HttpURLConnection 打开到 url 的连接,并将新连接的输出流重定向到客户端的输入流。

您需要分别处理 POST、DELETE 和其他 http 操作。

于 2012-10-14T14:07:06.583 回答