Java代理服务器无法加载整个网页,但成功加载了其中的一部分

时间:2015-04-11 06:42:45

标签: java web proxy server proxy-server

我的Java代理服务器无法加载整个网页,但成功加载了部分网页。 例如,当我转到网页时,连接会超时。但是,如果我只加载一个组件(例如http://www.abcde.com/abc.jpg),那就没关系。 对不起,代码有点长......

class ThreadPerConnect implements Runnable {
Socket clientSocket = null;

public ThreadPerConnect(Socket cs) {
    clientSocket = cs;
}

public void run() {
    try {
        System.out.println(clientSocket.getRemoteSocketAddress());
        StringBuilder sb = new StringBuilder();

        // handle IO in same thread now
        BufferedReader inFromClient = new BufferedReader(
                new InputStreamReader(clientSocket.getInputStream()));
        DataOutputStream outToClient = new DataOutputStream(
                clientSocket.getOutputStream());
        String request = inFromClient.readLine();
        // read request from client
        System.out.println(request);
        sb.append(request + "\r\n");
        String abc;

        System.out.println("inFromClient: ");
        while (!(abc = inFromClient.readLine()).isEmpty()) {
            sb.append(abc + "\r\n");
            System.out.println("abc is " + abc);
        }
        sb.append("\r\n");

        String finalS = sb.toString();
        System.out.println("finalS is: \n" + finalS);

        // get the url
        StringTokenizer st = new StringTokenizer(request);
        String requestType = st.nextToken(); // skip the "GET"
        String url = st.nextToken();
        System.out.println("Url is " + url);

        // remove "http://"
        if (url.startsWith("http://"))
            url = url.substring(7);

        String[] parts = url.split("/", 2);
        String ipRaw = parts[0];
        String[] parts2 = ipRaw.split(":");
        String ip = parts2[0];

        int port;
        if (parts2.length > 1)
            port = Integer.parseInt(parts2[1]);
        // get port number from request
        else
            port = 80;

        String fileName;
        if (parts.length > 1)
            fileName = "/" + parts[1];
        else
            fileName = "/";

        String httpVersion = st.nextToken();

        // get the filename

        System.out.printf(
                "Ip is %s\t \nfileName is %s \nhttpVersion is %s", ip,
                fileName, httpVersion);

        // go surf actual web
        Socket anotherSocket = new Socket(ip, port);

        System.out.println("\ncreate anotherSocket success");

        PrintWriter out = new PrintWriter(anotherSocket.getOutputStream());
        DataInputStream in = new DataInputStream(
                anotherSocket.getInputStream());
        out.print(finalS);
        out.flush();

        // getting actual content
        String x;
        int contentLength = 0;

        while (true) {
            x = in.readLine();

            if (x == null)
                break;
            if (x.isEmpty()) {
                outToClient.writeBytes("\r\n");
                System.out.println("END HEAD");
                break;
            }
            outToClient.writeBytes(x + "\r\n");
            System.out.println("x = " + x);
            outToClient.flush();

            if (x.startsWith("Content-Length:")) {
                contentLength = Integer.parseInt((x.split(": "))[1]);
            }
        }

        byte[] content = new byte[contentLength];
        in.readFully(content);

        outToClient.write(content, 0, contentLength);
        outToClient.flush();
        System.out.println("write done: " + contentLength + " bytes");

        // now write back information to client
        in.close();
        out.close();
        anotherSocket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

0 个答案:

没有答案