我的java简单缓存代理无法正常工作

时间:2016-04-16 16:43:57

标签: java http-proxy

当我在我的代理设置中使用端口80编译此代码时,它说:

  

需要端口号作为参数   Java结果:-1

页面没有加载,但没有错误。有人可以解决这个问题吗?

这是我的ProxyCache.java代码

public class ProxyCache {
    /** Port for the proxy */
    private static int port;
    /** Socket for client connections */
    private static ServerSocket socket;
    private static Socket client = null;

    public static HashMap<String, HttpResponse> cache = new HashMap<String, HttpResponse>();

    /** Create the ProxyCache object and the socket */
    public static void init(int p) {
        port = p;
        try {
            socket = new ServerSocket(port);
        } catch (IOException e) {
            System.out.println("Error creating socket: " + e);
            System.exit(-1);
        }
    }

    /** Read command line arguments and start proxy
     * @param args */
    public static void main(String args[]) {
        int myPort = 0;

        try {
            myPort = Integer.parseInt(args[0]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Need port number as argument");
            System.exit(-1);
        } catch (NumberFormatException e) {
            System.out.println("Please give port number as integer.");
            System.exit(-1);
        }

        init(myPort);

        /** Main loop. Listen for incoming connections and spawn a new
         * thread for handling them */

        while (true) {
            try {
                client = socket.accept();

                ThreadHandler thread = new ThreadHandler(client);
                thread.run();

            } catch (IOException e) {
                System.out.println("Error reading request from client: " + e);
                /* Definitely cannot continue processing this request,
                 * so skip to next iteration of while loop. */
                continue;
            }
        }
    }
}

这是我发送请求的方式:

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

public class HttpRequest {
    /** Help variables */
    final static String CRLF = "\r\n";
    final static int HTTP_PORT = 80;
    /** Store the request parameters */
    String method;
    String URI;
    String version;
    String headers = "";
    /** Server and port */
    private String host;
    private int port;

    /** Create HttpRequest by reading it from the client socket */
    public HttpRequest(BufferedReader from) {
    String firstLine = "";
    String[] tmp;
    try {
        firstLine = from.readLine();
    } catch (IOException e) {
        System.out.println("Error reading request line: " + e);
    }

    if(firstLine != null)
    {
        tmp = firstLine.split(" ");
        method = tmp[0];
        URI = tmp[1];
        version = tmp[2];

        System.out.println("URI is: " + URI);

        if (!method.equals("GET")) {
            System.out.println("Error: Method not GET");
        }

        try {
            String line = from.readLine();
            if(line.length() != 0)
            {
                while (line.length() != 0) {
                    headers += line + CRLF;
                    /* We need to find host header to know which server to
                     * contact in case the request URI is not complete. */
                    if (line.startsWith("Host:")) {
                        tmp = line.split(" ");
                        if (tmp[1].indexOf(':') > 0) {
                            String[] tmp2 = tmp[1].split(":");
                            host = tmp2[0];
                            port = Integer.parseInt(tmp2[1]);
                        } else {
                            host = tmp[1];
                            port = HTTP_PORT;
                        }
                    }

                    line = from.readLine();
                }
            }
            else
            {
                if (tmp[1].indexOf(':') > 0) {
                    String[] tmp2 = tmp[1].split(":");

                    if(tmp2[1].indexOf(':') > 0)
                    {   
                        String[] tmp3 = tmp2[1].split(":");
                        URI = "/";
                        host = tmp3[0].substring(2);
                        port = Integer.parseInt(tmp3[1]);
                    }
                    else
                    {
                        URI = "/";
                        host = tmp2[1].substring(2);
                        port = HTTP_PORT;
                    }
                } else {
                    URI = "/";
                    host = tmp[1];
                    port = HTTP_PORT;
                }
                headers += "Host: " + host + CRLF;
                headers += "Port: " + port + CRLF;
            }

        } catch (IOException e) {
            System.out.println("Error reading from socket: " + e);
            return;
        }

        System.out.println("Host to contact is: " + host + " at port " + port);
        }
    }

    /** Return host for which this request is intended */
    public String getHost() {
        return host;
    }

    /** Return port for server */
    public int getPort() {
        return port;
    }

    /**
     * Convert request into a string for easy re-sending.
     */
    public String toString() {
        String req = "";

        req = method + " " + URI + " " + version + CRLF;
        req += headers;
        /* This proxy does not support persistent connections */
        req += "Connection: close" + CRLF;
        req += CRLF;

        return req;
    }
}

0 个答案:

没有答案