如果我使用程序或浏览器,我将获得不同的网站源代码

时间:2016-07-08 21:54:59

标签: java html http cookies web

使用此代码,我正在尝试获取完整的源代码:

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class URLConnectionReader extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    TextArea ta;

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {

            @Override
            public void handle(WindowEvent event) {
                System.out.println("hallo");
                System.exit(0);
            }
        });

        Button btn = new Button();
        System.out.println("1");
        btn.setText("Go!");
        btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("2");
            try {
                show();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Hello World!");

        }
    });

    ta = new TextArea();
    ta.setFocusTraversable(false);
    ta.setEditable(false);

    HBox comps = new HBox();
    comps.getChildren().add(btn);
    comps.getChildren().add(ta);

    StackPane root = new StackPane();
    root.getChildren().add(comps);

    primaryStage.setScene(new Scene(root, 900, 950));
    primaryStage.show();
}

@SuppressWarnings("resource")
public void show() throws IOException {
    System.out.println("3");
    URL url = new URL(
            "http://www.backpack.tf/classifieds?quality=6&tradable=1&craftable=1&australium=-1&quality=5");

    URLConnection conn = url.openConnection();
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

    Scanner scanner = new Scanner(new InputStreamReader(conn.getInputStream()));
    System.out.println("4");

    while (scanner.hasNextLine()) {
        ta.appendText(scanner.nextLine() + "\n");
        System.out.println(scanner.nextLine());
    }
}

}

显示网站Oracle's documentation的源代码。 该程序显示一个输出,但如果我用firefox打开相同的页面并查看那里的网站源代码,它会更长。如何让我的程序输出整个代码?我是否必须使用Coocies或使用户代理更大?

0 个答案:

没有答案