0

我被编写了 fowling 代码,然后在 127.0.0.1:9090 上设置了 HTTP 代理并尝试访问 google.com,但它打印以下输出并且没有任何反应。输出:

Waiting for clients on port 9090
Got connection from /127.0.0.1:11827
Active Connections = 1
Waiting for clients on port 9090
connect started
connect finished
***Got connection from /74.125.232.131:80
writed
GET http://google.com/ HTTP/1.1
Host: google.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: ****
Connection: keep-alive

null

read line startes

代码:

import java.net.*;
import java.io.*;
import java.util.*;
public class SocketGoogler
{
    public static void main(String[] args)
    {
        int port = 9090;
        try {
            ServerSocket server = new ServerSocket(port);
            //System.out.println(google());
            while(true) {
                System.out.println("Waiting for clients on port " + port);
                Socket client = server.accept();
                ConnectionHandler handler = new ConnectionHandler(client);
                handler.start();
            }
        } catch(Exception ex) {
            System.out.println("Connection error: "+ex);
        }
    }

}
class ConnectionHandler extends Thread {
    private Socket client;
    BufferedReader reader;
    PrintWriter writer;
    static int count;
    public ConnectionHandler(Socket client) {
        this.client = client;
        System.out.println("Got connection from "+client.getInetAddress()
                +":"+client.getPort());
        count++;
        System.out.println("Active Connections = " + count);
    }
    public void run() {
        String message;
        String totalMessage;
        try {
            reader = new BufferedReader(new
                    InputStreamReader(client.getInputStream()));
            writer = new PrintWriter(client.getOutputStream());
            writer.flush();
            message = reader.readLine();
            totalMessage=message+"\n";
            while (message != null) {
                message = reader.readLine();
                totalMessage+=message+"\n";
            }
            client.close();
            //System.out.println(totalMessage);
            google(totalMessage);
            count--;
            System.out.println("Active Connections = " + count);
        } catch (Exception ex) {
            count--;
            System.out.println("Active Connections = " + count);
        }
    }
    public String  google(String w) throws IOException
    {
        String message , totalMessage;
        int port = 80;
        //ServerSocket server = new ServerSocket(port,10,InetAddress.getByName("google.com"));
        //Socket googler=server.accept();
        Socket googler=new Socket();
        InetSocketAddress endpoint=new InetSocketAddress(InetAddress.getByName("google.com").getHostAddress(), port);
        System.out.println("connect started");
        googler.connect(endpoint);
        System.out.println("connect finished");
        System.out.println("***Got connection from "+googler.getInetAddress()+":"+googler.getPort());
        BufferedReader reader = new BufferedReader(new
                InputStreamReader(googler.getInputStream()));
        PrintWriter writer=new PrintWriter(googler.getOutputStream());
        //String w="GET http://google.com/ HTTP/1.1\nHost: google.com\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\nAccept-Language: en-US,en;q=0.5\nAccept-Encoding: gzip, deflate\nConnection: keep-alive"+null;
        writer.print(w);
        System.out.println("writed\n"+w);
        System.out.println("read line startes");
        message = reader.readLine();
        System.out.println("read line finished");
        totalMessage=message+"\n";
        while (message != null) {
            message = reader.readLine();
            System.out.println("*");
            totalMessage+=message+"\n";
        }
        googler.close();
        System.out.println("close");
        return totalMessage;
    }
}

为什么会出现这个问题?
我已经连接了 google.com 并发送请求,但该主机没有任何响应。

4

1 回答 1

0

使用writer.flush();
它将发送您的请求。:)

于 2013-07-04T03:20:12.830 回答