简体   繁体   English

如何使用Java从互联网读取文本文件?

[英]How can I read a text file from the internet with Java?

I want to read the second line of the text at this URL: " http://vuln2014.picoctf.com:51818/ " (this is a capture-the-flag competition but only asking for flags or direction to flags breaks the competition rules). 我想在以下URL上阅读文本的第二行:“ http://vuln2014.picoctf.com:51818/ ”(这是“ 旗比赛”,但仅询问标志或指向标志的方向会破坏比赛)规则)。 I am attempting to open an input stream from the URL but I get an Invalid HTTP Response exception. 我试图从URL打开输入流,但收到无效的HTTP响应异常。 Any help is appreciated, and I recognize that my error is likely quite foolish. 感谢您的帮助,我知道我的错误可能很愚蠢。

Code: 码:

URL url = new URL("http://vuln2014.picoctf.com:51818");
URLConnection con = url.openConnection();
InputStream is = con.getInputStream()

The error occurs at the third line. 该错误发生在第三行。 java.io.IOException: Invalid Http response at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1342) at name.main(name.java:41) java.io.IOException:在sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1342)处的name.main(name.java:41)处的Http响应无效

curl happily gets the text from the page, and it is perfectly accessible from a web browser. curl可以很高兴地从页面中获取文本,并且可以从Web浏览器中完美访问它。

When you do this: 执行此操作时:

URL url = new URL("http://vuln2014.picoctf.com:51818");
URLConnection con = url.openConnection();

You are entering into a contract that says that this URL uses the http protocol. 您正在签订合同,说该URL使用http协议。 When you call openConnection it expects to get http responses because you used http:// in the URL as the protocol. 当您调用openConnection它希望获得http响应,因为您在URL中使用了http://作为协议。 The Java Documentation says: Java文档说:

If for the URL's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. 如果对于URL的协议(例如HTTP或JAR),存在一个公共的专用URLConnection子类,它属于以下软件包之一或它们的子软件包之一:java.lang,java.io,java.util,java.net,返回的连接将属于该子类。 For example, for HTTP an HttpURLConnection will be returned , and for JAR a JarURLConnection will be returned. 例如,对于HTTP,将返回HttpURLConnection ;对于JAR,将返回JarURLConnection。

The server you are connecting to just returns a couple lines of data. 您要连接的服务器仅返回几行数据。 I retrieved them with the command nc vuln2014.picoctf.com 51818 . 我使用命令nc vuln2014.picoctf.com 51818检索了它们。 There is no http response code like HTTP/1.1 200 OK : 没有 HTTP/1.1 200 OK 这样的 http响应代码:

Welcome to the Daedalus Corp Spies RSA Key Generation Service. 欢迎使用Daedalus Corp间谍RSA密钥生成服务。 The public modulus you should use to send your updates is below. 您用于发送更新的公共模数如下。 Remember to use exponent 65537. b4ab920c4772c5247e7d89ec7570af7295f92e3b584fc1a1a5624d19ca07cd72ab4ab9c8ec58a63c09f382aa319fa5a714a46ffafcb6529026bbc058fc49fb1c29ae9f414db4aa609a5cab6ff5c7b4c4cfc7c18844f048e3899934999510b2fe25fcf8c572514dd2e14c6e19c4668d9ad82fe647cf9e700dcf6dc23496be30bb 请记住,使用指数65537 b4ab920c4772c5247e7d89ec7570af7295f92e3b584fc1a1a5624d19ca07cd72ab4ab9c8ec58a63c09f382aa319fa5a714a46ffafcb6529026bbc058fc49fb1c29ae9f414db4aa609a5cab6ff5c7b4c4cfc7c18844f048e3899934999510b2fe25fcf8c572514dd2e14c6e19c4668d9ad82fe647cf9e700dcf6dc23496be30bb

In this case I would use java.net.Socket to establish a connection and then read the lines. 在这种情况下,我将使用java.net.Socket建立连接,然后读取各行。 This is a simplistic approach that assumes there are 2 lines of data: 这是一种简单的方法,假定有两行数据:

Socket theSocket;
try {
    theSocket = new Socket("vuln2014.picoctf.com", 51818);
    BufferedReader inFile = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));

    String strGreet = inFile.readLine();
    String strData = inFile.readLine();
} catch (IOException e) {
    e.printStackTrace();
}

As for why curl and browsers may render it properly? 至于为什么curl和浏览器可以正确渲染呢? They are likely more lenient about the data they read and will just dump what is read from the port even if it doesn't conform to the specified protocol (like http ) 他们可能对读取的数据更宽容,即使不符合指定协议(例如http )的情况,也只会转储从端口读取的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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