List of usage examples for java.net HttpURLConnection setFixedLengthStreamingMode
public void setFixedLengthStreamingMode(long contentLength)
From source file:fr.mixit.android.utils.NetworkUtils.java
@TargetApi(Build.VERSION_CODES.GINGERBREAD) static ResponseHttp getInputStreamFromHttpUrlConnection(String url, boolean isPost, String args) { final ResponseHttp myResponse = new ResponseHttp(); HttpURLConnection urlConnection = null; if (cookieManager == null) { cookieManager = new CookieManager(); CookieHandler.setDefault(cookieManager); }/*from w w w . j ava2 s . com*/ try { String urlWithParams = url; if (!isPost && args != null) { urlWithParams = getGetURL(url, args); } final URL urlObject = new URL(urlWithParams); urlConnection = (HttpURLConnection) urlObject.openConnection(); urlConnection.setConnectTimeout(CONNECTION_TIMEOUT); urlConnection.setReadTimeout(SOCKET_TIMEOUT); // if (cookieSession != null) { // cookieManager.getCookieStore().removeAll(); // cookieManager.getCookieStore().addCookie(cookieSession); // } if (isPost) { urlConnection.setDoOutput(true); } // urlConnection.connect(); if (isPost && args != null) { final byte[] params = args.getBytes(HTTP.UTF_8); urlConnection.setFixedLengthStreamingMode(params.length);// or urlConnection.setChunkedStreamingMode(0); final OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); out.write(params); } myResponse.status = urlConnection.getResponseCode(); if (DEBUG_MODE) { Log.d(TAG, "Status code : " + myResponse.status); } myResponse.jsonText = getJson(urlConnection.getInputStream()); } catch (final MalformedURLException e) { if (DEBUG_MODE) { Log.e(TAG, "The URL is malformatted", e); } } catch (final IllegalAccessError e) { if (DEBUG_MODE) { Log.e(TAG, "setDoOutput after openning a connection or already done"); } } catch (final UnsupportedEncodingException e) { if (DEBUG_MODE) { Log.e(TAG, "UTF8 unsupported for args", e); } } catch (final IllegalStateException e) { if (DEBUG_MODE) { Log.e(TAG, "I/O Error", e); } } catch (final IllegalArgumentException e) { if (DEBUG_MODE) { Log.e(TAG, "I/O Error", e); } } catch (final IOException e) { if (DEBUG_MODE) { Log.e(TAG, "I/O Error", e); } } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return myResponse; }
From source file:models.cloud.notifications.gcm.Sender.java
/** * Make an HTTP post to a given URL.// www.j a va2 s . co m * * @return HTTP response. */ protected HttpURLConnection post(String url, String contentType, String body) throws IOException { if (url == null || body == null) { throw new IllegalArgumentException("arguments cannot be null"); } play.Logger.info(url); if (!url.startsWith("https://")) { logger.warning("URL does not use https: " + url); } logger.fine("Sending POST to " + url); logger.finest("POST body: " + body); byte[] bytes = body.getBytes(HTTP.UTF_8); HttpURLConnection conn = getConnection(url); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("Authorization", "key=" + key); OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); return conn; }
From source file:uk.ac.gate.cloud.data.DataBundle.java
/** * Upload a file to an open bundle.// w w w. ja va 2s .co m * * @param fileName the name to use for the bundle entry * @param contentLength the number of bytes to upload * @param source an input stream from which the file's content can be * read. It must provide exactly <code>contentLength</code> * bytes up to end-of-file. The stream will be read to EOF * but will not be closed by this method, the caller is * responsible for ensuring the stream is properly closed. */ public void addFile(String fileName, int contentLength, InputStream source) { ObjectNode request = JsonNodeFactory.instance.objectNode(); request.put("fileName", fileName); // create the input AddResult addRes = client.post(url + "/add", new TypeReference<AddResult>() { }, request); try { // upload the file HttpURLConnection putConnection = (HttpURLConnection) addRes.putUrl.openConnection(); putConnection.setDoOutput(true); putConnection.setRequestMethod("PUT"); putConnection.setRequestProperty("Content-Type", "application/octet-stream"); putConnection.setFixedLengthStreamingMode(contentLength); OutputStream out = putConnection.getOutputStream(); try { IOUtils.copy(source, out); } finally { IOUtils.closeQuietly(out); } } catch (IOException e) { throw new RestClientException(e); } }
From source file:us.dustinj.locationstore.io.LocationExporter.java
protected int post(URL url, byte[] payload) { HttpURLConnection conn = null; int returnCode = 400; try {// ww w . j a v a 2 s.c om // this does no network IO. conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(10000); // tells HUC that you're going to POST; still no IO. conn.setDoOutput(true); conn.setDoInput(true); conn.setFixedLengthStreamingMode(payload.length); // still no IO InputStream in; OutputStream out; // this opens a connection, then sends POST & headers. out = conn.getOutputStream(); // At this point, the client may already have received a 4xx // or 5xx error, but dont you dare call getResponseCode() // or HUC will hit you with an exception. // now we can send the body out.write(payload); // NOW you can look at the status. returnCode = conn.getResponseCode(); if (returnCode / 100 != 2) { // Dear me, dear me } // presumably youre interested in the response body // Unlike the identical call in the previous example, this provokes no network IO. in = conn.getInputStream(); Log.d(this.getClass().getSimpleName(), convertStreamToString(in)); } catch (IOException e) { try { returnCode = conn.getResponseCode(); } catch (IOException e1) { e1.printStackTrace(); } } finally { if (conn != null) { conn.disconnect(); // Let's practice good hygiene } } return returnCode; }
From source file:es.tor.push.android.Sender.java
protected HttpURLConnection post(String url, String contentType, String body) throws IOException { if (url == null || body == null) { throw new IllegalArgumentException("arguments cannot be null"); }/*from w w w . jav a 2 s. c om*/ if (!url.startsWith("https://")) { logger.warning("URL does not use https: " + url); } logger.fine("Sending POST to " + url); logger.finest("POST body: " + body); byte[] bytes = body.getBytes(); HttpURLConnection conn = getConnection(url); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("Authorization", "key=" + key); OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); return conn; }
From source file:com.google.android.gcm.server.Sender.java
protected HttpURLConnection post(String url, String contentType, String body) throws IOException { if (url == null || body == null) { throw new IllegalArgumentException("arguments cannot be null"); }/*from w ww . ja va2s . co m*/ if (!url.startsWith("https://")) { logger.warning("URL does not use https: " + url); } logger.fine("Sending POST to " + url); logger.finest("POST body: " + body); byte[] bytes = body.getBytes(UTF8); HttpURLConnection conn = getConnection(url); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("Authorization", "key=" + key); OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); return conn; }
From source file:org.cru.godtools.api.notifications.Sender.java
/** * Makes an HTTP POST request to a given endpoint. * <p/>//from w w w.j av a2 s . c om * <p/> * <strong>Note: </strong> the returned connected should not be disconnected, * otherwise it would kill persistent connections made using Keep-Alive. * * @param url endpoint to post the request. * @param contentType type of request. * @param body body of the request. * @return the underlying connection. * @throws IOException propagated from underlying methods. */ protected HttpURLConnection post(String url, String contentType, String body) throws IOException { if (url == null || body == null) { throw new IllegalArgumentException("arguments cannot be null"); } if (!url.startsWith("https://")) { log.warn("URL does not use https: " + url); } log.info("Sending POST to " + url); log.info("POST body: " + body); byte[] bytes = body.getBytes(); HttpURLConnection conn = getConnection(url); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("Authorization", "key=" + key); OutputStream out = conn.getOutputStream(); try { out.write(bytes); } finally { close(out); } return conn; }
From source file:be.appfoundry.custom.google.android.gcm.server.Sender.java
/** * Makes an HTTP POST request to a given endpoint. * <p/>/*from w w w. j a va2 s . c o m*/ * <p/> * <strong>Note: </strong> the returned connected should not be disconnected, * otherwise it would kill persistent connections made using Keep-Alive. * * @param url endpoint to post the request. * @param contentType type of request. * @param body body of the request. * @return the underlying connection. * @throws IOException propagated from underlying methods. */ protected HttpURLConnection post(String url, String contentType, String body) throws IOException { if (url == null || contentType == null || body == null) { throw new IllegalArgumentException("arguments cannot be null"); } if (!url.startsWith("https://")) { logger.warning("URL does not use https: " + url); } logger.fine("Sending POST to " + url); logger.finest("POST body: " + body); byte[] bytes = body.getBytes(UTF8); HttpURLConnection conn = getConnection(url); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("Authorization", "key=" + key); OutputStream out = conn.getOutputStream(); try { out.write(bytes); } finally { close(out); } return conn; }
From source file:ccs_server.Sender.java
/** * Makes an HTTP POST request to a given endpoint. * * <p>/*from w w w . j a v a 2 s .co m*/ * <strong>Note: </strong> the returned connected should not be disconnected, * otherwise it would kill persistent connections made using Keep-Alive. * * @param url endpoint to post the request. * @param contentType type of request. * @param body body of the request. * * @return the underlying connection. * * @throws IOException propagated from underlying methods. */ protected HttpURLConnection post(String url, String contentType, String body) throws IOException { if (url == null || body == null) { throw new IllegalArgumentException("arguments cannot be null"); } if (!url.startsWith("https://")) { logger.warning("URL does not use https: " + url); } logger.fine("Sending POST to " + url); logger.finest("POST body: " + body); byte[] bytes = body.getBytes(); HttpURLConnection conn = getConnection(url); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("Authorization", "key=" + key); OutputStream out = conn.getOutputStream(); try { out.write(bytes); } finally { close(out); } return conn; }