Example usage for java.net HttpURLConnection setFixedLengthStreamingMode

List of usage examples for java.net HttpURLConnection setFixedLengthStreamingMode

Introduction

In this page you can find the example usage for java.net HttpURLConnection setFixedLengthStreamingMode.

Prototype

public void setFixedLengthStreamingMode(long contentLength) 

Source Link

Document

This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance.

Usage

From source file:com.emc.ecs.smart.SmartUploader.java

/**
 * Does a standard PUT upload using HttpURLConnection.
 *///from   w  w w  . ja va  2  s.  c  o m
private void doSimpleUpload() {
    try {
        fileSize = Files.size(fileToUpload);
        HttpURLConnection conn = null;
        long start = System.currentTimeMillis();
        try (DigestInputStream is = new DigestInputStream(Files.newInputStream(fileToUpload),
                MessageDigest.getInstance("MD5"))) {
            conn = (HttpURLConnection) uploadUrl.openConnection();
            conn.setFixedLengthStreamingMode(fileSize);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setInstanceFollowRedirects(false);
            conn.setUseCaches(false);
            conn.setRequestMethod(HttpMethod.PUT);
            conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
            OutputStream os = conn.getOutputStream();
            byte[] buf = new byte[CHUNK_SIZE];
            int len;

            while ((len = is.read(buf)) != -1) {
                os.write(buf, 0, len);
                bytesUploaded += len;
                printPercent();
            }
            os.close();

            if (conn.getResponseCode() != ClientResponse.Status.OK.getStatusCode()) {
                throw new RuntimeException("Unable to upload object content: status=" + conn.getResponseCode());
            } else {
                List<String> eTags = conn.getHeaderFields().get(HttpHeaders.ETAG);
                if (eTags == null || eTags.size() < 1) {
                    throw new RuntimeException("Unable to get ETag for uploaded data");
                } else {
                    byte[] sentMD5 = is.getMessageDigest().digest();
                    String eTag = eTags.get(0);
                    byte[] gotMD5 = DatatypeConverter.parseHexBinary(eTag.substring(1, eTag.length() - 1));
                    if (!Arrays.equals(gotMD5, sentMD5)) {
                        throw new RuntimeException("ETag doesn't match streamed data's MD5.");
                    }
                }
            }
        } catch (IOException e) {
            throw new Exception("IOException while uploading object content after writing " + bytesUploaded
                    + " of " + fileSize + " bytes: " + e.getMessage());
        } finally {
            if (conn != null)
                conn.disconnect();
        }

        long elapsed = System.currentTimeMillis() - start;
        printRate(fileSize, elapsed);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.dell.asm.asmcore.asmmanager.util.discovery.DeviceTypeCheckUtil.java

/**
 * HTTP POST with basic auth//  www  .j  a  v  a  2 s .  com
 *
 * @param urlToRead device URL
 * @return http response message
 * @throws IOException
 */
public static String httpPost(String urlToRead, String username, String password) throws IOException {
    URL url;
    HttpURLConnection conn;
    BufferedReader rd = null;
    String line;
    StringBuffer result = new StringBuffer();

    try {
        url = new URL(urlToRead);
        conn = (HttpURLConnection) url.openConnection();
        if (conn instanceof HttpsURLConnection) {
            HttpsURLConnection sslConn = (HttpsURLConnection) conn;
            sslConn.setHostnameVerifier(hv);
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, new TrustManager[] { tmNoCheck }, new SecureRandom());
            sslConn.setSSLSocketFactory(sslContext.getSocketFactory());
        }
        conn.setDoOutput(true);
        conn.setConnectTimeout(AsmManagerApp.CONNECT_TIMEOUT); // timeout value
        conn.setReadTimeout(AsmManagerApp.CONNECT_TIMEOUT);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("x-dell-api-version", "2.0");
        conn.setRequestProperty("Authorization", encodeCredentials(username, password));
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setFixedLengthStreamingMode("{}".length());
        conn.getOutputStream().write("{}".getBytes(Charset.forName("UTF-8")));

        rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
    } catch (RuntimeException e) {
        throw new IOException("Could not connect to the url: " + e.getMessage());
    } catch (Exception e) {
        throw new IOException("Could not connect to the url: " + urlToRead);
    } finally {
        if (rd != null)
            rd.close();
    }
    return result.toString();
}

From source file:com.checkmarx.jenkins.CxWebService.java

private CxWSResponseRunID sendScanRequest(final FilePath base64ZipFile, String soapActionName,
        Pair<byte[], byte[]> soapMessage, XmlResponseParser xmlResponseParser) throws AbortException {
    try {/*ww w.j ava 2  s  .  c  o  m*/

        // Create HTTP connection

        final HttpURLConnection streamingUrlConnection = (HttpURLConnection) webServiceUrl.openConnection();
        streamingUrlConnection.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
        streamingUrlConnection.addRequestProperty("SOAPAction",
                String.format("\"http://Checkmarx.com/v7/%s\"", soapActionName));
        streamingUrlConnection.setDoOutput(true);
        // Calculate the length of the soap message
        final long length = soapMessage.getLeft().length + soapMessage.getRight().length
                + base64ZipFile.length();
        streamingUrlConnection.setFixedLengthStreamingMode((int) length);
        streamingUrlConnection.connect();
        final OutputStream os = streamingUrlConnection.getOutputStream();

        logger.info("Uploading sources to Checkmarx server");
        os.write(soapMessage.getLeft());
        final InputStream fis = base64ZipFile.read();
        org.apache.commons.io.IOUtils.copyLarge(fis, os);

        os.write(soapMessage.getRight());
        os.close();
        fis.close();
        logger.info("Finished uploading sources to Checkmarx server");

        CxWSResponseRunID cxWSResponseRunID = xmlResponseParser.parse(streamingUrlConnection.getInputStream());

        if (!cxWSResponseRunID.isIsSuccesfull()) {
            String message = "Submission of sources for scan failed: \n" + cxWSResponseRunID.getErrorMessage();
            throw new AbortException(message);
        }

        return cxWSResponseRunID;

    } catch (HttpRetryException e) {
        String consoleMessage = "\nCheckmarx plugin for Jenkins does not support Single sign-on authentication."
                + "\nPlease, configure Checkmarx server to work in Anonymous authentication mode.\n";
        logger.error(consoleMessage);
        throw new AbortException(e.getMessage());
    } catch (IOException | JAXBException | XMLStreamException | InterruptedException e) {
        logger.error(e.getMessage(), e);
        throw new AbortException(e.getMessage());
    }
}

From source file:com.daidiansha.acarry.control.network.core.volley.toolbox.HurlStack.java

/**
 * Perform a multipart request on a connection
 *
 * @param connection The Connection to perform the multi part request
 * @param request    The params to add to the Multi Part request
 *                   The files to upload
 * @throws ProtocolException/*from w  w  w . j av a2s . co m*/
 */
private void setConnectionParametersForMultipartRequest(HttpURLConnection connection, MultiPartRequest request)
        throws IOException, ProtocolException {

    final String charset = request.getProtocolCharset();
    final int curTime = (int) (System.currentTimeMillis() / 1000);
    final String boundary = BOUNDARY_PREFIX + curTime;
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty(HEADER_CONTENT_TYPE, String.format(CONTENT_TYPE_MULTIPART, charset, curTime));

    Map<String, MultiPartParam> multipartParams = request.getMultipartParams();
    Map<String, String> filesToUpload = request.getFilesToUpload();

    if (request.isFixedStreamingMode()) {
        int contentLength = getContentLengthForMultipartRequest(boundary, multipartParams, filesToUpload);

        connection.setFixedLengthStreamingMode(contentLength);
    } else {
        connection.setChunkedStreamingMode(0);
    }
    // Modified end
    PrintWriter writer = null;
    try {
        OutputStream out = connection.getOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(out, charset), true);

        for (String key : multipartParams.keySet()) {
            MultiPartParam param = multipartParams.get(key);

            writer.append(boundary).append(CRLF)
                    .append(String.format(HEADER_CONTENT_DISPOSITION + COLON_SPACE + FORM_DATA, key))
                    .append(CRLF).append(HEADER_CONTENT_TYPE + COLON_SPACE + param.contentType).append(CRLF)
                    .append(CRLF).append(param.value).append(CRLF).flush();
        }
        long allFileSize = 0;
        long allUpLoadSize = 0;
        int fileLocation = 0;
        for (String key : filesToUpload.keySet()) {
            File file = new File(filesToUpload.get(key));

            if (!file.exists() || file.isDirectory()) {
                continue;
            }
            allFileSize += file.length();
        }
        for (String key : filesToUpload.keySet()) {

            File file = new File(filesToUpload.get(key));

            if (!file.exists()) {
                throw new IOException(String.format("File not found: %s", file.getAbsolutePath()));
            }

            if (file.isDirectory()) {
                throw new IOException(String.format("File is a directory: %s", file.getAbsolutePath()));
            }

            writer.append(boundary).append(CRLF)
                    .append(String.format(
                            HEADER_CONTENT_DISPOSITION + COLON_SPACE + FORM_DATA + SEMICOLON_SPACE + FILENAME,
                            key, file.getName()))
                    .append(CRLF).append(HEADER_CONTENT_TYPE + COLON_SPACE + CONTENT_TYPE_OCTET_STREAM)
                    .append(CRLF).append(HEADER_CONTENT_TRANSFER_ENCODING + COLON_SPACE + BINARY).append(CRLF)
                    .append(CRLF).flush();

            BufferedInputStream input = null;
            try {
                FileInputStream fis = new FileInputStream(file);
                int transferredBytes = 0;
                int totalSize = (int) file.length();
                input = new BufferedInputStream(fis);
                int bufferLength = 0;

                byte[] buffer = new byte[1024];
                int progress = 0;
                while ((bufferLength = input.read(buffer)) > 0) {
                    out.write(buffer, 0, bufferLength);
                    transferredBytes += bufferLength;
                    allUpLoadSize += bufferLength;
                    int p = transferredBytes * 100 / totalSize;
                    if (p != progress) {
                        progress = p;
                        request.fileProgress(filesToUpload.get(key), fileLocation, transferredBytes, totalSize,
                                allUpLoadSize, allFileSize);
                    }
                }
                out.flush(); // Important! Output cannot be closed. Close of
                // writer will close
                // output as well.
            } finally {
                if (input != null)
                    try {
                        input.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
            }
            writer.append(CRLF).flush(); // CRLF is important! It indicates
            fileLocation++;
            // end of binary
            // boundary.
        }

        // End of multipart/form-data.
        writer.append(boundary + BOUNDARY_PREFIX).append(CRLF).flush();

    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:org.shredzone.flattr4j.connector.impl.FlattrConnection.java

@Override
public Collection<FlattrObject> result() throws FlattrException {
    try {//w  ww . j  a  va 2 s .co  m
        String queryString = (queryParams != null ? "?" + queryParams : "");

        URL url;
        if (call != null) {
            url = new URI(baseUrl).resolve(call + queryString).toURL();
        } else {
            url = new URI(baseUrl + queryString).toURL();
        }

        HttpURLConnection conn = createConnection(url);
        conn.setRequestMethod(type.name());
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Accept-Charset", ENCODING);
        conn.setRequestProperty("Accept-Encoding", "gzip");

        if (token != null) {
            conn.setRequestProperty("Authorization", "Bearer " + token.getToken());
        } else if (key != null) {
            conn.setRequestProperty("Authorization", "Basic " + base64(key.getKey() + ':' + key.getSecret()));
        }

        byte[] outputData = null;
        if (data != null) {
            outputData = data.toString().getBytes(ENCODING);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setFixedLengthStreamingMode(outputData.length);
        } else if (formParams != null) {
            outputData = formParams.toString().getBytes(ENCODING);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setFixedLengthStreamingMode(outputData.length);
        }

        LOG.info("Sending Flattr request: {0}", call);
        conn.connect();

        if (outputData != null) {
            OutputStream out = conn.getOutputStream();
            try {
                out.write(outputData);
            } finally {
                out.close();
            }
        }

        if (limit != null) {
            String remainingHeader = conn.getHeaderField("X-RateLimit-Remaining");
            if (remainingHeader != null) {
                limit.setRemaining(Long.parseLong(remainingHeader));
            } else {
                limit.setRemaining(null);
            }

            String limitHeader = conn.getHeaderField("X-RateLimit-Limit");
            if (limitHeader != null) {
                limit.setLimit(Long.parseLong(limitHeader));
            } else {
                limit.setLimit(null);
            }

            String currentHeader = conn.getHeaderField("X-RateLimit-Current");
            if (currentHeader != null) {
                limit.setCurrent(Long.parseLong(currentHeader));
            } else {
                limit.setCurrent(null);
            }

            String resetHeader = conn.getHeaderField("X-RateLimit-Reset");
            if (resetHeader != null) {
                limit.setReset(new Date(Long.parseLong(resetHeader) * 1000L));
            } else {
                limit.setReset(null);
            }
        }

        List<FlattrObject> result;

        if (assertStatusOk(conn)) {
            // Status is OK and there is content
            Object resultData = new JSONTokener(readResponse(conn)).nextValue();
            if (resultData instanceof JSONArray) {
                JSONArray array = (JSONArray) resultData;
                result = new ArrayList<FlattrObject>(array.length());
                for (int ix = 0; ix < array.length(); ix++) {
                    FlattrObject fo = new FlattrObject(array.getJSONObject(ix));
                    result.add(fo);
                    LOG.verbose("<- JSON result: {0}", fo);
                }
                LOG.verbose("<-   {0} rows", array.length());
            } else if (resultData instanceof JSONObject) {
                FlattrObject fo = new FlattrObject((JSONObject) resultData);
                result = Collections.singletonList(fo);
                LOG.verbose("<- JSON result: {0}", fo);
            } else {
                throw new MarshalException("unexpected result type " + resultData.getClass().getName());
            }
        } else {
            // Status was OK, but there is no content
            result = Collections.emptyList();
        }

        return result;
    } catch (URISyntaxException ex) {
        throw new IllegalStateException("bad baseUrl");
    } catch (IOException ex) {
        throw new FlattrException("API access failed: " + call, ex);
    } catch (JSONException ex) {
        throw new MarshalException(ex);
    } catch (ClassCastException ex) {
        throw new FlattrException("Unexpected result type", ex);
    }
}

From source file:com.odoko.solrcli.actions.CrawlPostAction.java

/**
 * Reads data from the data stream and posts it to solr,
 * writes to the response to output/*from w  w  w.jav a  2s.c  om*/
 * @return true if success
 */
public boolean postData(InputStream data, Integer length, OutputStream output, String type, URL url) {
  boolean success = true;
  if(type == null)
    type = DEFAULT_CONTENT_TYPE;
  HttpURLConnection urlc = null;
  try {
    try {
      urlc = (HttpURLConnection) url.openConnection();
      try {
        urlc.setRequestMethod("POST");
      } catch (ProtocolException e) {
        fatal("Shouldn't happen: HttpURLConnection doesn't support POST??"+e);
      }
      urlc.setDoOutput(true);
      urlc.setDoInput(true);
      urlc.setUseCaches(false);
      urlc.setAllowUserInteraction(false);
      urlc.setRequestProperty("Content-type", type);

      if (null != length) urlc.setFixedLengthStreamingMode(length);

    } catch (IOException e) {
      fatal("Connection error (is Solr running at " + solrUrl + " ?): " + e);
      success = false;
    }
        
    OutputStream out = null;
    try {
      out = urlc.getOutputStream();
      pipe(data, out);
    } catch (IOException e) {
      fatal("IOException while posting data: " + e);
      success = false;
    } finally {
      try { if(out!=null) out.close(); } catch (IOException x) { /*NOOP*/ }
    }
        
    InputStream in = null;
    try {
      if (HttpURLConnection.HTTP_OK != urlc.getResponseCode()) {
        warn("Solr returned an error #" + urlc.getResponseCode() + 
              " " + urlc.getResponseMessage());
        success = false;
      }

      in = urlc.getInputStream();
      pipe(in, output);
    } catch (IOException e) {
      warn("IOException while reading response: " + e);
      success = false;
    } finally {
      try { if(in!=null) in.close(); } catch (IOException x) { /*NOOP*/ }
    }
        
  } finally {
    if(urlc!=null) urlc.disconnect();
  }
  return success;
}

From source file:com.salesmanBuddy.dao.JDBCSalesmanBuddyDAO.java

public GoogleToken getValidTokenForUser(String googleUserId, Users user)
        throws GoogleRefreshTokenResponseException {
    if (user == null)
        user = this.getUserByGoogleId(googleUserId);

    GoogleToken gt = this.getTokenForUserFromCache(user.getId());
    if (gt != null)
        return gt;

    String iosString = "client_secret=" + GoogleClientSecretiOS + "&grant_type=refresh_token"
            + "&refresh_token=" + user.getRefreshToken() + "&client_id=" + GoogleClientIdiOS;
    String webString = "refresh_token=" + user.getRefreshToken() + "&client_id=" + GoogleClientIdWeb
            + "&client_secret=" + GoogleClientSecretWeb + "&grant_type=refresh_token";
    String androidString = "refresh_token=" + user.getRefreshToken() + "&client_id=" + GoogleClientIdAndroid
            + "&client_secret=" + GoogleClientSecretAndroid + "&grant_type=refresh_token";

    /*/*from ww w . j av a 2s  .co m*/
     * 
     * client_id=8819981768.apps.googleusercontent.com&
    client_secret={client_secret}&
    refresh_token=1/6BMfW9j53gdGImsiyUH5kU5RsR4zwI9lUVX-tqf8JXQ&
    grant_type=refresh_token
            
            
            
    url: https://accounts.google.com/o/oauth2/auth, params:access_type=offline&client_id=38235450166-dgbh1m7aaab7kopia2upsdj314odp8fc.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.me%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email
     */
    byte[] body = null;

    if (user.getDeviceType() == 1)
        body = iosString.getBytes();
    else if (user.getDeviceType() == 2)
        body = webString.getBytes();
    else if (user.getDeviceType() == 3)
        body = androidString.getBytes();
    else
        throw new RuntimeException("the user's device type doesnt conform to any known types, their type: "
                + user.getDeviceType());

    URL url;
    JSONObject json = null;
    try {
        url = new URL(GoogleRefreshTokenEndpoint);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setFixedLengthStreamingMode(body.length);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.getOutputStream().write(body);
        body = IOUtils.toByteArray(conn.getInputStream());
        json = new JSONObject(new String(body));
    } catch (MalformedURLException e) {
        throw new RuntimeException("malformedUrlException: " + e.getLocalizedMessage());
    } catch (IOException e) {
        // TODO make this error handling more comprehensive, if refreshtoken is invalid we need to be able to handle it
        JDBCSalesmanBuddyDAO.sendErrorToMe("couldnt exchange refresh token for googleUserId: " + googleUserId
                + ", error: " + e.getLocalizedMessage());
        String jsonString = "";
        throw new RuntimeException("!IOException: " + e.getLocalizedMessage() + ", deviceType:"
                + user.getDeviceType() + ", " + new String(body) + ", json: " + jsonString);
    } catch (JSONException jse) {
        throw new RuntimeException("JSONException: " + jse.getLocalizedMessage());
    }

    GoogleRefreshTokenResponse grtr = new GoogleRefreshTokenResponse(json);
    // put token in database for caching
    this.saveGoogleTokenInCache(grtr, user);
    return this.getTokenForUserFromCache(user.getId());
}

From source file:com.BeatYourRecord.SubmitActivity.java

private ResumeInfo resumeFileUpload(String uploadUrl)
        throws IOException, ParserConfigurationException, SAXException, Internal500ResumeException {
    HttpURLConnection urlConnection = getGDataUrlConnection(uploadUrl);
    urlConnection.setRequestProperty("Content-Range", "bytes */*");
    urlConnection.setRequestMethod("POST");
    urlConnection.setRequestProperty("X-HTTP-Method-Override", "PUT");
    urlConnection.setFixedLengthStreamingMode(0);

    HttpURLConnection.setFollowRedirects(false);

    urlConnection.connect();//ww w.j ava  2 s .c o  m
    int responseCode = urlConnection.getResponseCode();

    if (responseCode >= 300 && responseCode < 400) {
        int nextByteToUpload;
        String range = urlConnection.getHeaderField("Range");
        if (range == null) {
            Log.d(LOG_TAG, String.format("PUT to %s did not return 'Range' header.", uploadUrl));
            nextByteToUpload = 0;
        } else {
            Log.d(LOG_TAG, String.format("Range header is '%s'.", range));
            String[] parts = range.split("-");
            if (parts.length > 1) {
                nextByteToUpload = Integer.parseInt(parts[1]) + 1;
            } else {
                nextByteToUpload = 0;
            }
        }
        return new ResumeInfo(nextByteToUpload);
    } else if (responseCode >= 200 && responseCode < 300) {
        return new ResumeInfo(parseVideoId(urlConnection.getInputStream()));
    } else if (responseCode == 500) {
        // TODO this is a workaround for current problems with resuming uploads while switching transport (Wifi->EDGE)
        throw new Internal500ResumeException(
                String.format("Unexpected response for PUT to %s: %s " + "(code %d)", uploadUrl,
                        urlConnection.getResponseMessage(), responseCode));
    } else {
        throw new IOException(String.format("Unexpected response for PUT to %s: %s " + "(code %d)", uploadUrl,
                urlConnection.getResponseMessage(), responseCode));
    }
}

From source file:org.openspaces.pu.container.servicegrid.deploy.Deploy.java

private void uploadPU(String puPath, File puFile) throws IOException {
    if (puFile.length() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(
                "File " + puFile.getPath() + " is too big: " + puFile.length() + " bytes");
    }//from ww  w  . j  a va2 s  .c  o m
    byte[] buffer = new byte[4098];
    String codebase = getCodebase(deployAdmin);
    info("Uploading [" + puPath + "] " + "[" + puFile.getPath() + "] to [" + codebase + "]");
    HttpURLConnection conn = (HttpURLConnection) new URL(codebase + puFile.getName()).openConnection();
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setAllowUserInteraction(false);
    conn.setUseCaches(false);
    conn.setRequestMethod("PUT");
    conn.setRequestProperty("Extract", "true");
    //Sets the Content-Length request property
    //And disables buffering of file in memory (pure streaming)
    conn.setFixedLengthStreamingMode((int) puFile.length());
    conn.connect();
    OutputStream out = new BufferedOutputStream(conn.getOutputStream());
    InputStream in = new BufferedInputStream(new FileInputStream(puFile));
    int byteCount = 0;
    int bytesRead = -1;
    while ((bytesRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
        byteCount += bytesRead;
    }
    out.flush();
    out.close();
    in.close();

    int responseCode = conn.getResponseCode();

    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    StringBuffer sb = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    reader.close();
    conn.disconnect();
    if (responseCode != 200 && responseCode != 201) {
        throw new RuntimeException(
                "Failed to upload file, response code [" + responseCode + "], response: " + sb.toString());
    }
}