0

この Web サイトhttp://movies.about.com/od/actorsalphalist/Actors_Detailed_Movie_News_Interviews_Websites.htmから HTML を収集しようとしています。

ソケットを開き、HTML ページの各行を読み取って印刷しようとします。実行すると、結果として「EOF is false」と「1」のみが表示されます。

これは別の例で機能するはずなので、何が間違っているのかまったくわかりません...助けてくれてありがとう!

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

public class Twitter {

    static final int DEFAULT_PORT = 80;

    protected DataInputStream reply = null;
    protected PrintStream send = null;
    protected Socket sock = null;

    // ***********************************************************
    // *** The constructors create the socket and set up the input
    // *** and output channels on that socket.

    public Twitter() throws UnknownHostException, IOException {
        this(DEFAULT_PORT);
    }

    public Twitter(int port) throws UnknownHostException, IOException {
        sock = new Socket("movies.about.com", port);
        System.out.println(sock);
        reply = new DataInputStream(sock.getInputStream());
        System.out.println();
        send = new PrintStream(sock.getOutputStream());
    }

    // ***********************************************************
    // *** forecast uses the socket that has already been created
    // *** to carry on a conversation with the Web server that it
    // *** has been contacted through the socket.

    public void forecast() {
        int i;
        String HTMLline;
        boolean eof, gotone;

        // *** This issues the same query that a Web browser would issue
        // *** to the Web server.

        try {
            send.println("GET /od/actorsalphalist/Actors_Detailed_Movie_News_Interviews_Websites.htm HTTP/1.1");
        } catch (Exception e) {
            System.out.println("about.com server is down.");
        }

        // *** This section parses the response from the Web server.
        // *** NOTE THAT "real" EOF does not occur until the Web server
        // *** has closed the connection.

        eof = false;
        gotone = false;
        while (!eof) {
            System.out.println("EOF is false");
            try {
                System.out.println("1");
                HTMLline = reply.readLine();
                System.out.println("2");
                System.out.println(HTMLline);
                System.out.println("Here?");
                if (HTMLline != null) {
                    System.out.println("its not null");
                }
                if (HTMLline == null) {
                    System.out.println("WTFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
                } else {
                    eof = true;
                    System.out.println("is it?");
                }
            } catch (Exception e) {
                System.out.println("this exception happend");
                e.printStackTrace();
                eof = true;
            }
        }
    }

    // ***********************************************************
    // *** We need to close the socket when this class is destroyed.

    protected void finalize() throws Throwable {
        sock.close();
    }

    // ***********************************************************
    // *** The main program creates a new Twitter class and
    // *** sends that class the command line args (via findNumber).

    public static void main(String[] args) {
        Twitter aboutCom;
        DataInputStream cin = new DataInputStream(System.in);

        try {
            aboutCom = new Twitter();
            aboutCom.forecast();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
4

1 に答える 1

1

有効な HTTP リクエストをまだ送信していないため、サーバーはリクエストの完了を待っています。GET 行は \r\n で終了する必要があり、要求ヘッダーを区切るために空白行として別の行が必要です。

ただし、冗長に HTTP を自分で再実装しようとするのではなく、URL、openConnection()、getInputStream() などを使用する必要があります。あなたがやっているように、あなたが道を間違える可能性があるだけです。

于 2013-01-21T03:56:35.563 に答える