Example usage for java.net URLConnection setDoOutput

List of usage examples for java.net URLConnection setDoOutput

Introduction

In this page you can find the example usage for java.net URLConnection setDoOutput.

Prototype

public void setDoOutput(boolean dooutput) 

Source Link

Document

Sets the value of the doOutput field for this URLConnection to the specified value.

Usage

From source file:skinsrestorer.shared.utils.MojangAPI.java

private static URLConnection setupConnection(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    connection.setConnectTimeout(10000);
    connection.setReadTimeout(10000);// w ww .ja v  a 2 s .c  om
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}

From source file:org.openlegacy.designtime.newproject.NewProjectMetadataRetriever.java

private static InputStream getUrlConnectionInputStream(String urlPath) throws IOException {
    URL url = new URL(urlPath);
    URLConnection con = url.openConnection();
    con.setDoInput(true);//ww w  .j ava  2s.  co m
    con.setDoOutput(false);
    con.connect();

    return con.getInputStream();
}

From source file:simpleserver.thread.Statistics.java

private static String getRemoteContent(URL url, JSONObject data) throws IOException {
    URLConnection con = url.openConnection();
    if (data != null) {
        String post = "data=" + URLEncoder.encode(data.toString(), "UTF-8");
        con.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
        writer.write(post);/*from  ww w.  ja v  a  2s.c  o m*/
        writer.flush();
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

    StringBuilder content = new StringBuilder();

    while (reader.ready()) {
        content.append(reader.readLine());
    }

    return content.toString();
}

From source file:com.linkedin.pinot.controller.helix.ControllerTest.java

public static String sendPostRequest(String urlString, String payload)
        throws UnsupportedEncodingException, IOException, JSONException {
    LOGGER.info("Sending POST to " + urlString + " with payload " + payload);
    final long start = System.currentTimeMillis();
    final URL url = new URL(urlString);
    final URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));

    writer.write(payload, 0, payload.length());
    writer.flush();/*from w  w w .java 2 s.c om*/
    final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

    final StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    final long stop = System.currentTimeMillis();

    LOGGER.info(" Time take for Request : " + payload + " in ms:" + (stop - start));

    return sb.toString();
}

From source file:Downloader.java

/**
 * Creates an URL connection for the specified URL and data.
 * //  w  w w  .jav a2  s.  c om
 * @param url The URL to connect to
 * @param postData The POST data to pass to the URL
 * @return An URLConnection for the specified URL/data
 * @throws java.net.MalformedURLException If the specified URL is malformed
 * @throws java.io.IOException If an I/O exception occurs while connecting
 */
private static URLConnection getConnection(final String url, final String postData)
        throws MalformedURLException, IOException {
    final URL myUrl = new URL(url);
    final URLConnection urlConn = myUrl.openConnection();

    urlConn.setUseCaches(false);
    urlConn.setDoInput(true);
    urlConn.setDoOutput(postData.length() > 0);
    urlConn.setConnectTimeout(10000);

    if (postData.length() > 0) {
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        final DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());
        out.writeBytes(postData);
        out.flush();
        out.close();
    }

    return urlConn;
}

From source file:sturesy.util.web.WebCommands2.java

/**
 * Sends a post to the specified url//from  w ww. j  av  a 2 s.  c  o m
 * 
 * @param url
 *            URL
 * @param data
 *            Base64 encoded JSONObject
 * @param hashed
 *            Hash of data used for verification
 * @return response
 * @throws IOException
 */
public static String sendPost(URL url, String data, String hashed) throws IOException {
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);

    if (connection instanceof HttpURLConnection) {
        ((HttpURLConnection) connection).setRequestMethod("POST");
    } else if (connection instanceof HttpsURLConnection) {
        ((HttpsURLConnection) connection).setRequestMethod("POST");
    }

    OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
    wr.write("data=" + data + "&hash=" + hashed);
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;

    StringBuffer buffer = new StringBuffer();
    while ((line = rd.readLine()) != null) {
        buffer.append(line);
    }
    wr.close();
    rd.close();

    return buffer.toString();
}

From source file:ru.codeinside.gses.webui.form.JsonForm.java

static String loadTemplate(ActivitiApp app, String ref) {
    try {//from   w w  w  .j a va2s . co  m
        URL serverUrl = app.getServerUrl();
        URL url = new URL(serverUrl, ref);
        logger().info("fetch template " + url);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(false);
        connection.setDoInput(true);
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        connection.setUseCaches(false);
        connection.connect();
        return Streams.toString(connection.getInputStream());
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:UploadUtils.ImgurUpload.java

/**
 * Connect to image host./*from ww  w  . j  ava 2 s . c o  m*/
 * @return the connection
 * @throws java.MalformedURLExceptionMalformedURLException if the URL can't be constructed successfully
 * @throws java.IOException if failed to open connection to the newly constructed URL
 */
private static URLConnection connect() throws MalformedURLException, IOException {
    URLConnection conn = null;
    try {
        // opens connection and sends data
        URL url = new URL(IMGUR_POST_URI);
        conn = url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Authorization", "Client-ID " + IMGUR_API_KEY);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    } catch (MalformedURLException ex) {
        throw ex;
    } catch (IOException ex) {
        throw ex;
    }
    return conn;
}

From source file:info.magnolia.cms.exchange.simple.Transporter.java

/**
 * http form multipart form post//from  w  w w  .  j a va  2  s  . c  om
 * @param connection
 * @param activationContent
 * @throws ExchangeException
 */
public static void transport(URLConnection connection, ActivationContent activationContent)
        throws ExchangeException {
    FileInputStream fis = null;
    DataOutputStream outStream = null;
    try {
        byte[] buffer = new byte[BUFFER_SIZE];
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty("Content-type", "multipart/form-data; boundary=" + BOUNDARY);
        connection.setRequestProperty("Cache-Control", "no-cache");

        outStream = new DataOutputStream(connection.getOutputStream());
        outStream.writeBytes("--" + BOUNDARY + "\r\n");

        // set all resources from activationContent
        Iterator fileNameIterator = activationContent.getFiles().keySet().iterator();
        while (fileNameIterator.hasNext()) {
            String fileName = (String) fileNameIterator.next();
            fis = new FileInputStream(activationContent.getFile(fileName));
            outStream.writeBytes("content-disposition: form-data; name=\"" + fileName + "\"; filename=\""
                    + fileName + "\"\r\n");
            outStream.writeBytes("content-type: application/octet-stream" + "\r\n\r\n");
            while (true) {
                synchronized (buffer) {
                    int amountRead = fis.read(buffer);
                    if (amountRead == -1) {
                        break;
                    }
                    outStream.write(buffer, 0, amountRead);
                }
            }
            fis.close();
            outStream.writeBytes("\r\n" + "--" + BOUNDARY + "\r\n");
        }
        outStream.flush();
        outStream.close();

        log.debug("Activation content sent as multipart/form-data");
    } catch (Exception e) {
        throw new ExchangeException(
                "Simple exchange transport failed: " + ClassUtils.getShortClassName(e.getClass()), e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                log.error("Exception caught", e);
            }
        }
        if (outStream != null) {
            try {
                outStream.close();
            } catch (IOException e) {
                log.error("Exception caught", e);
            }
        }
    }

}

From source file:com.buglabs.dragonfly.util.WSHelper.java

/**
 * Post contents of input stream to URL.
 * /*from w  ww  .  ja  va 2  s . c o m*/
 * @param url
 * @param stream
 * @return
 * @throws IOException
 */
protected static String postBase64(URL url, InputStream stream) throws IOException {
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    byte[] buff = streamToByteArray(stream);

    String em = Base64.encodeBytes(buff);
    OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
    osr.write(em);
    osr.flush();

    stream.close();
    conn.getOutputStream().flush();
    conn.getOutputStream().close();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line, resp = new String("");
    while ((line = rd.readLine()) != null) {
        resp = resp + line + "\n";
    }
    rd.close();

    return resp;
}