Friday, 15 March 2013

stream - How can I read a text file from the internet with Java? -



stream - How can I read a text file from the internet with Java? -

i want read sec line of text @ url: "http://vuln2014.picoctf.com:51818/" (this capture-the-flag competition asking flags or direction flags breaks competition rules). attempting open input stream url invalid http response exception. help appreciated, , recognize error quite foolish.

code:

url url = new url("http://vuln2014.picoctf.com:51818"); urlconnection con = url.openconnection(); inputstream = con.getinputstream()

the error occurs @ 3rd line. java.io.ioexception: invalid http response @ sun.net.www.protocol.http.httpurlconnection.getinputstream(httpurlconnection.java:1342) @ name.main(name.java:41)

curl happily gets text page, , accessible web browser.

when this:

url url = new url("http://vuln2014.picoctf.com:51818"); urlconnection con = url.openconnection();

you entering contract says url uses http protocol. when phone call openconnection expects http responses because used http:// in url protocol. java documentation says:

if url's protocol (such http or jar), there exists public, specialized urlconnection subclass belonging 1 of next packages or 1 of subpackages: java.lang, java.io, java.util, java.net, connection returned of subclass. for example, http httpurlconnection returned, , jar jarurlconnection returned.

the server connecting returns couple lines of data. retrieved them command nc vuln2014.picoctf.com 51818. there no http response code like http/1.1 200 ok:

welcome daedalus corp spies rsa key generation service. public modulus should utilize send updates below. remember utilize exponent 65537. b4ab920c4772c5247e7d89ec7570af7295f92e3b584fc1a1a5624d19ca07cd72ab4ab9c8ec58a63c09f382aa319fa5a714a46ffafcb6529026bbc058fc49fb1c29ae9f414db4aa609a5cab6ff5c7b4c4cfc7c18844f048e3899934999510b2fe25fcf8c572514dd2e14c6e19c4668d9ad82fe647cf9e700dcf6dc23496be30bb

in case utilize java.net.socket found connection , read lines. simplistic approach assumes there 2 lines of data:

socket thesocket; seek { thesocket = new socket("vuln2014.picoctf.com", 51818); bufferedreader infile = new bufferedreader(new inputstreamreader(thesocket.getinputstream())); string strgreet = infile.readline(); string strdata = infile.readline(); } grab (ioexception e) { e.printstacktrace(); }

as why curl , browsers may render properly? more lenient info read , dump read port if doesn't conform specified protocol (like http)

java stream

No comments:

Post a Comment