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:org.wso2.carbon.connector.integration.test.common.ConnectorIntegrationUtil.java

public static int sendRequestToRetriveHeaders(String addUrl, String query, String contentType)
        throws IOException, JSONException {

    String charset = "UTF-8";
    URLConnection connection = new URL(addUrl).openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Accept-Charset", charset);
    connection.setRequestProperty("Content-Type", contentType + ";charset=" + charset);

    OutputStream output = null;/*w  ww .  j  a  v a 2 s. c  o m*/
    try {
        output = connection.getOutputStream();
        output.write(query.getBytes(charset));
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException logOrIgnore) {
                log.error("Error while closing the connection");
            }
        }
    }

    HttpURLConnection httpConn = (HttpURLConnection) connection;
    int responseCode = httpConn.getResponseCode();

    return responseCode;
}

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

protected static String post(URL url, String payload, Map props) throws IOException {
    String propstr = new String();

    for (Iterator i = props.keySet().iterator(); i.hasNext();) {
        String key = (String) i.next();
        propstr = propstr + URLEncoder.encode(key, "UTF-8") + "="
                + URLEncoder.encode((String) props.get(key), "UTF-8");
        if (i.hasNext()) {
            propstr = propstr + "&";
        }//  w w  w  .  j  a  v a  2  s  .c o  m
    }

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
    osr.write(propstr);
    osr.flush();

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

    return resp;
}

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

protected static String post(URL url, Map props) throws IOException {
    String propstr = new String();

    for (Iterator i = props.keySet().iterator(); i.hasNext();) {
        String key = (String) i.next();
        propstr = propstr + URLEncoder.encode(key, "UTF-8") + "="
                + URLEncoder.encode((String) props.get(key), "UTF-8");
        if (i.hasNext()) {
            propstr = propstr + "&";
        }/*w  w w  .j ava2s .c o m*/
    }

    SSLUtils.verifyHost();

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
    osr.write(propstr);
    osr.flush();

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

    return resp;
}

From source file:net.daboross.bukkitdev.skywars.gist.GistReport.java

public static String gistText(Logger logger, String text) {
    URL postUrl;/*from  w  w w.j  ava  2  s.  c  om*/
    try {
        postUrl = new URL("https://api.github.com/gists");
    } catch (MalformedURLException ex) {
        logger.log(Level.FINE, "Non severe error encoding api.github.com URL", ex);
        return null;
    }
    URLConnection connection;
    try {
        connection = postUrl.openConnection();
    } catch (IOException ex) {
        logger.log(Level.FINE, "Non severe error opening api.github.com connection", ex);
        return null;
    }
    connection.setDoOutput(true);
    connection.setDoInput(true);
    JSONStringer outputJson = new JSONStringer();
    try {
        outputJson.object().key("description").value("SkyWars debug").key("public").value("false").key("files")
                .object().key("report.md").object().key("content").value(text).endObject().endObject()
                .endObject();
    } catch (JSONException ex) {
        logger.log(Level.FINE, "Non severe error while writing report", ex);
        return null;
    }
    String jsonOuptutString = outputJson.toString();
    try (OutputStream outputStream = connection.getOutputStream()) {
        try (OutputStreamWriter requestWriter = new OutputStreamWriter(outputStream)) {
            requestWriter.append(jsonOuptutString);
            requestWriter.close();
        }
    } catch (IOException ex) {
        logger.log(Level.FINE, "Non severe error writing report", ex);
        return null;
    }

    JSONObject inputJson;
    try {
        inputJson = new JSONObject(readConnection(connection));
    } catch (JSONException | IOException unused) {
        logger.log(Level.FINE, "Non severe error while reading response for report.", unused);
        return null;
    }
    String resultUrl = inputJson.optString("html_url", null);
    return resultUrl == null ? null : shortenURL(logger, resultUrl);
}

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

protected static InputStream getAsStream(URL url) throws IOException {
    URLConnection conn = url.openConnection();

    conn.setDoInput(true);//  w  w w . j a v  a 2s  .c  o  m
    conn.setDoOutput(false);

    return conn.getInputStream();
}

From source file:org.wso2.carbon.connector.integration.test.common.ConnectorIntegrationUtil.java

public static JSONObject sendRequest(String addUrl, String query) throws IOException, JSONException {

    String charset = "UTF-8";
    URLConnection connection = new URL(addUrl).openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Accept-Charset", charset);
    connection.setRequestProperty("Content-Type", "application/json;charset=" + charset);
    OutputStream output = null;//from  w  ww  .  ja  va2 s .c  o m
    try {
        output = connection.getOutputStream();
        output.write(query.getBytes(charset));
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException logOrIgnore) {
                log.error("Error while closing the connection");
            }
        }
    }

    HttpURLConnection httpConn = (HttpURLConnection) connection;
    InputStream response;

    if (httpConn.getResponseCode() >= 400) {
        response = httpConn.getErrorStream();
    } else {
        response = connection.getInputStream();
    }

    String out = "{}";
    if (response != null) {
        StringBuilder sb = new StringBuilder();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = response.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, len));
        }

        if (!sb.toString().trim().isEmpty()) {
            out = sb.toString();
        }
    }

    JSONObject jsonObject = new JSONObject(out);

    return jsonObject;
}

From source file:xtrememp.update.SoftwareUpdate.java

public static Version getLastVersion(URL url) throws Exception {
    Version result = null;/* www  .  j  av a  2 s .com*/
    InputStream urlStream = null;
    try {
        URLConnection urlConnection = url.openConnection();
        urlConnection.setAllowUserInteraction(false);
        urlConnection.setConnectTimeout(30000);
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(false);
        urlConnection.setReadTimeout(10000);
        urlConnection.setUseCaches(true);
        urlStream = urlConnection.getInputStream();
        Properties properties = new Properties();
        properties.load(urlStream);

        result = new Version();
        result.setMajorNumber(Integer.parseInt(properties.getProperty("xtrememp.lastVersion.majorNumber")));
        result.setMinorNumber(Integer.parseInt(properties.getProperty("xtrememp.lastVersion.minorNumber")));
        result.setMicroNumber(Integer.parseInt(properties.getProperty("xtrememp.lastVersion.microNumber")));
        result.setVersionType(
                Version.VersionType.valueOf(properties.getProperty("xtrememp.lastVersion.versionType")));
        result.setReleaseDate(properties.getProperty("xtrememp.lastVersion.releaseDate"));
        result.setDownloadURL(properties.getProperty("xtrememp.lastVersion.dounloadURL"));
    } finally {
        IOUtils.closeQuietly(urlStream);
    }
    return result;
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Delete hero from database//from  w w  w . j av  a 2 s  .  co  m
 * @param number    hero id 
 */
private static void deleteHero(String number) {
    try {
        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/hero/delete/" + number);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while (in.readLine() != null) {
        }
        System.out.println("Hero with id: " + number + " was deleted");
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when deleting hero");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Delete role from database/*  ww w  . j a v  a2 s.  com*/
 * @param number    role id
 */
private static void deleteRole(String number) {
    try {
        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/role/delete/" + number);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while (in.readLine() != null) {
        }
        System.out.println("Role with id: " + number + " was deleted");
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when deleting role");
        System.out.println(e);
    }
}

From source file:XmlUtils.java

public static Document parse(URL url) throws DocumentException, IOException {
    URLConnection urlConnection;
    DataInputStream inStream;// w ww  . ja v a 2s .  c om
    urlConnection = url.openConnection();
    ((HttpURLConnection) urlConnection).setRequestMethod("GET");

    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(false);
    urlConnection.setUseCaches(false);
    inStream = new DataInputStream(urlConnection.getInputStream());

    byte[] bytes = new byte[1024];
    int read;
    StringBuilder builder = new StringBuilder();
    while ((read = inStream.read(bytes)) >= 0) {
        String readed = new String(bytes, 0, read, "UTF-8");
        builder.append(readed);
    }
    SAXReader reader = new SAXReader();

    XmlUtils.createIgnoreErrorHandler(reader);
    //        InputSource inputSource = new InputSource(new InputStreamReader(inStream, "UTF-8"));
    //        inputSource.setEncoding("UTF-8");
    Document dom = reader.read(new StringReader(builder.toString()));
    inStream.close();
    //        new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("retrieval.xml"), "UTF-8")
    return dom;
}