Java从网站获取源代码

时间:2023-01-18 23:53:30

This code is in a class and it gets the source code from a website. I would like get the code and print it out in a jTextArea that is in another class, but i have no idea how i can do that. how can i export this source code in another class?

此代码位于类中,它从网站获取源代码。我想获取代码并将其打印在另一个类的jTextArea中,但我不知道我该怎么做。如何在另一个类中导出此源代码?

public static void Connect() throws Exception{

    URL url = new URL("https://www.google.com/");
    URLConnection spoof = url.openConnection();


    spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
    BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
    String strLine = "";

    while ((strLine = in.readLine()) != null){

        System.out.println(strLine);
    }

}

in the other class I have a button with this code:

在另一个类中,我有一个带有此代码的按钮:

    try{
        Connect();
    } catch(Exception e) {

    }

1 个解决方案

#1


In the second class, you would call

在第二节课,你会打电话

try{
    ClassA.Connect();
} catch(Exception e) {

}

Where ClassA is the name of the class where public static void Connect() is defined. Note that, by convention, the method name should begin with a lower case, so it should be public static void connect().

其中ClassA是定义public static void Connect()的类的名称。请注意,按照惯例,方法名称应以小写字母开头,因此它应该是public static void connect()。

#1


In the second class, you would call

在第二节课,你会打电话

try{
    ClassA.Connect();
} catch(Exception e) {

}

Where ClassA is the name of the class where public static void Connect() is defined. Note that, by convention, the method name should begin with a lower case, so it should be public static void connect().

其中ClassA是定义public static void Connect()的类的名称。请注意,按照惯例,方法名称应以小写字母开头,因此它应该是public static void connect()。