使用java代理服务器缓存网页

时间:2015-03-17 05:04:30

标签: java proxy server webpage

我正在尝试使用java学习客户端/服务器。需要你帮助的人。提前谢谢。

我构建了一个连接到浏览器的代理服务器,我想要做的是当它连接到浏览器时它会缓存或下载浏览器访问过的网页。可能吗?您可以建议的任何提示或材料?如果可能的话,示例代码将是完美的,因为我真的不知道如何去做。

这是我的代理服务器的代码。

import java.io.*;
import java.net.*;
import java.util.*;

public class test extends Thread {

Socket connectedClient = null;  
BufferedReader inFromClient = null; // request from the client (browser)
DataOutputStream outToClient = null; // response to client (browser)


public test(Socket client) {
    connectedClient = client;
}           

public void run() {

    try {

        System.out.println( "The Client "+
        connectedClient.getInetAddress() + ":" + connectedClient.getPort() + " is connected");

        inFromClient = new BufferedReader(new InputStreamReader (connectedClient.getInputStream()));                  
        outToClient = new DataOutputStream(connectedClient.getOutputStream());

        String requestString = inFromClient.readLine();
        String headerLine = requestString;

        StringTokenizer tokenizer = new StringTokenizer(headerLine);
        String httpMethod = tokenizer.nextToken();
        String httpQueryString = tokenizer.nextToken();

        StringBuffer responseBuffer = new StringBuffer();
        responseBuffer.append("The HTTP Client request is ....<BR>");

        System.out.println("The HTTP request string is ....");
        while (inFromClient.ready())
       {
            // Read the HTTP complete HTTP Query
            responseBuffer.append(requestString + "<BR>");
            System.out.println(requestString);
            requestString = inFromClient.readLine();
        }        

        if (httpMethod.equals("GET")) {

          if (httpQueryString.equals("/")) {
            // The default home page
            httprequest(200, responseBuffer.toString());                            
          } else {
            //filename : request from the client 
            String fileName = httpQueryString;

            fileName = URLDecoder.decode(fileName);
            System.out.println("Request:" + fileName);
            httprequest(200,fileName);                              
          }

        } 
        else httprequest(404, "<b>The Requested resource not found ....</b>");              
    } catch (Exception e) {
        e.printStackTrace();
    }       
}
public void httprequest(int statusCode,String location) throws Exception {

    HttpURLConnection connection = null;
    StringBuilder sb = null;
    String line = null;

    URL serverAddress = null;
    String statusLine = null;
    String serverdetails = "Server: Java ProxyServer";
    String contentTypeLine = "Content-type: text/html\n\n";

    if (statusCode == 200)
    {
                statusLine = "HTTP/1.1 200 OK" + "\r\n";
        try {
             serverAddress = new URL(location);

              connection = null;


              connection = (HttpURLConnection)serverAddress.openConnection();
              connection.setRequestMethod("GET");
              connection.setDoOutput(true);

              connection.connect();

              //read the result from the server
            InputStream rd = connection.getInputStream();
            outToClient.writeBytes(statusLine);
            outToClient.writeBytes(serverdetails);
            outToClient.writeBytes(contentTypeLine);
            outToClient.writeBytes("\r\n");


            byte[] buffer = new byte[1024] ;
            int bytesRead;

            while ((bytesRead = rd.read(buffer)) != -1 ){
            outToClient.write(buffer, 0, bytesRead);
            }
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (ProtocolException e) {
              e.printStackTrace();
          }
    }
    else{
        statusLine = "HTTP/1.1 404 Not Found" + "\r\n"; 
        outToClient.writeBytes(statusLine);
        outToClient.writeBytes(serverdetails);
        outToClient.writeBytes(contentTypeLine);

     }

  outToClient.close();
}


public static void main (String args[]) throws Exception {

    //System.setProperty("http.proxyHost","172.16.1.254") ;
   // System.setProperty("http.proxyPort", "3128") ;

    ServerSocket Server = new ServerSocket (8080, 0, InetAddress.getByName("localhost"));         
    System.out.println ("Proxy Server Waiting for client on port 8080");

    while(true) {                                   
            Socket connected = Server.accept();
            (new test(connected)).start();
    }      
}
}

1 个答案:

答案 0 :(得分:0)

我希望您希望监控HTTP流量,请参考此http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm,即使是我自己建立的,也可以从这种资源中获取参考资料。但是你无法进行SSL通信监控。