//login and get an auth token URL u = new URL("https://www.crushftp.com/"); HttpURLConnection urlc = (HttpURLConnection)u.openConnection(); urlc.setRequestMethod("POST"); urlc.setUseCaches(false); urlc.setDoOutput(true); urlc.getOutputStream().write(("command=login&username=demo&password=demo").getBytes("UTF8")); urlc.getResponseCode(); String cookie = urlc.getHeaderField("Set-Cookie"); String auth = cookie.substring(cookie.indexOf("CrushAuth=")+"CrushAuth=".length(),cookie.indexOf(";",cookie.indexOf("CrushAuth="))); urlc.disconnect(); //download a file using the auth token, and a post. Alternatively you could also just do a GET on the file. URL u = new URL("https://www.crushftp.com/"); HttpURLConnection urlc = (HttpURLConnection)u.openConnection(); urlc.setRequestMethod("POST"); urlc.setRequestProperty("Cookie", "CrushAuth="+auth+";"); urlc.setUseCaches(false); urlc.setDoInput(true); urlc.setDoOutput(true); urlc.getOutputStream().write(("command=download&path=/demo/KB2.txt").getBytes("UTF8")); int code = urlc.getResponseCode(); if (urlc.getURL().toExternalForm().indexOf("/WebInterface/login.html") >= 0) code = 302; if (code == 302) throw new Exception("Logged out".); InputStream in = new BufferedInputStream(urlc.getInputStream()); int bytesRead = 0; byte b[] = new byte[32768]; while (bytesRead >= 0) { bytesRead = in.read(b); if (bytesRead >= 0) out.write(b,0,bytesRead); } in.close(); urlc.disconnect(); //upload a file using the auth token, and a PUT. URL u = new URL("https://www.crushftp.com/demo/KB4.txt"); HttpURLConnection urlc = (HttpURLConnection)u.openConnection(); urlc.setRequestMethod("PUT"); urlc.setRequestProperty("Cookie", "CrushAuth="+auth+";"); urlc.setUseCaches(false); urlc.setChunkedStreamingMode(32768); urlc.setDoInput(true); urlc.setDoOutput(true); OutputStream out = urlc.getOutputStream(); int bytesRead = 0; byte b[] = new byte[32768]; while (bytesRead >= 0) { bytesRead = in.read(b); if (bytesRead >= 0) out.write(b,0,bytesRead); } out.close(); int code = urlc.getResponseCode(); if (urlc.getURL().toExternalForm().indexOf("/WebInterface/login.html") >= 0) code = 302; if (code == 302) throw new Exception("Logged out".); urlc.disconnect();
Add new attachment
Only authorized users are allowed to upload new attachments.
«
This particular version was published on 25-Oct-2018 04:31 by Ben Spink.
G’day (anonymous guest)
Log in
JSPWiki