Created
July 8, 2014 18:51
-
-
Save hartsock/f560457e37da6d209efb to your computer and use it in GitHub Desktop.
A snippet of Java code showing how to get data...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void getData(String urlString, String fileName) | |
| throws Exception { | |
| HttpURLConnection conn = null; | |
| URL urlSt = new URL(urlString); | |
| conn = (HttpURLConnection) urlSt.openConnection(); | |
| conn.setDoInput(true); | |
| conn.setDoOutput(true); | |
| conn.setAllowUserInteraction(true); | |
| // Maintain session | |
| List<String> cookies = (List<String>) headers.get("Set-cookie"); | |
| cookieValue = cookies.get(0); | |
| StringTokenizer tokenizer = new StringTokenizer(cookieValue, ";"); | |
| cookieValue = tokenizer.nextToken(); | |
| String pathData = "$" + tokenizer.nextToken(); | |
| String cookie = "$Version=\"1\"; " + cookieValue + "; " + pathData; | |
| // set the cookie in the new request header | |
| Map<String, List<String>> map = new HashMap<String, List<String>>(); | |
| map.put("Cookie", Collections.singletonList(cookie)); | |
| ((BindingProvider) vimPort).getRequestContext().put( | |
| MessageContext.HTTP_REQUEST_HEADERS, map); | |
| conn.setRequestProperty("Cookie", cookie); | |
| conn.setRequestProperty("Content-Type", "application/octet-stream"); | |
| conn.setRequestProperty("Expect", "100-continue"); | |
| conn.setRequestMethod("GET"); | |
| conn.setRequestProperty("Content-Length", "1024"); | |
| InputStream in = conn.getInputStream(); | |
| int leng = fileName.lastIndexOf("/"); | |
| String dir = fileName.substring(0, leng - 1); | |
| String fName = fileName.substring(leng + 1); | |
| fName = fName.replace("%20", " "); | |
| dir = replaceSpecialChar(dir); | |
| fileName = localPath + "/" + dir + "/" + fName; | |
| OutputStream out = | |
| new BufferedOutputStream(new FileOutputStream(fileName)); | |
| int bufLen = 9 * 1024; | |
| byte[] buf = new byte[bufLen]; | |
| byte[] tmp = null; | |
| int len = 0; | |
| @SuppressWarnings("unused") | |
| int bytesRead = 0; | |
| while ((len = in.read(buf, 0, bufLen)) != -1) { | |
| bytesRead += len; | |
| tmp = new byte[len]; | |
| System.arraycopy(buf, 0, tmp, 0, len); | |
| out.write(tmp, 0, len); | |
| } | |
| in.close(); | |
| out.close(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment