Example usage for java.net HttpURLConnection setDoInput

List of usage examples for java.net HttpURLConnection setDoInput

Introduction

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

Prototype

public void setDoInput(boolean doinput) 

Source Link

Document

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

Usage

From source file:Main.java

public static InputStream downloadURL(String link) throws IOException {
    URL url = new URL(link);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000);//from  www.  j a  v  a 2  s .c om
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.connect();
    logInfo("downloadStatus: " + conn.getResponseCode());
    return conn.getInputStream();
}

From source file:Main.java

public static String upLoad(File file, String RequestURL) {
    String BOUNDER = UUID.randomUUID().toString();
    String PREFIX = "--";
    String END = "/r/n";

    try {// ww w.ja  v  a2s  .  c  om
        URL url = new URL(RequestURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setReadTimeout(TIME_OUT);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Charset", CHARSET);
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cotent-Type", CONTENT_TYPE + ";boundary=" + BOUNDER);

        if (file != null) {
            OutputStream outputStream = connection.getOutputStream();

            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            StringBuffer sb = new StringBuffer();
            sb.append(PREFIX);
            sb.append(BOUNDER + END);
            dataOutputStream.write(sb.toString().getBytes());
            InputStream in = new FileInputStream(file);
            byte[] b = new byte[1024];
            int l = 0;
            while ((l = in.read()) != -1) {
                outputStream.write(b, 0, l);
            }
            in.close();
            dataOutputStream.write(END.getBytes());
            dataOutputStream.write((PREFIX + BOUNDER + PREFIX + END).getBytes());
            dataOutputStream.flush();

            int i = connection.getResponseCode();
            if (i == 200) {
                return SUCCESS;
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return FALIURE;

}

From source file:net.idlesoft.android.apps.github.utils.GravatarCache.java

private static Bitmap downloadGravatar(final String id) throws IOException {
    final URL aURL = new URL("http://www.gravatar.com/avatar/" + URLEncoder.encode(id) + "?size=100&d=mm");
    final HttpURLConnection conn = (HttpURLConnection) aURL.openConnection();
    conn.setDoInput(true);
    conn.connect();//from   w  ww .j a  v  a  2 s. co  m
    final InputStream is = conn.getInputStream();
    final Bitmap bm = BitmapFactory.decodeStream(is);
    is.close();
    return bm;
}

From source file:Main.java

public static String customrequest(String url, HashMap<String, String> params, String method) {
    try {/*from  www  .  j a va 2  s  . co m*/

        URL postUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) postUrl.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setConnectTimeout(5 * 1000);

        conn.setRequestMethod(method);
        conn.setUseCaches(false);
        conn.setInstanceFollowRedirects(true);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)");

        conn.connect();
        OutputStream out = conn.getOutputStream();
        StringBuilder sb = new StringBuilder();
        if (null != params) {
            int i = params.size();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (i == 1) {
                    sb.append(entry.getKey() + "=" + entry.getValue());
                } else {
                    sb.append(entry.getKey() + "=" + entry.getValue() + "&");
                }

                i--;
            }
        }
        String content = sb.toString();
        out.write(content.getBytes("UTF-8"));
        out.flush();
        out.close();
        InputStream inStream = conn.getInputStream();
        String result = inputStream2String(inStream);
        conn.disconnect();
        return result;
    } catch (Exception e) {
        // TODO: handle exception
    }
    return null;
}

From source file:Main.java

private static HttpURLConnection initHttpURLConn(String requestURL)
        throws MalformedURLException, IOException, ProtocolException {
    URL url = new URL(requestURL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(TIME_OUT);
    connection.setReadTimeout(TIME_OUT);
    connection.setDoInput(true);
    connection.setDoOutput(true);//  w w  w .  j a v a2  s  .  com
    connection.setUseCaches(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Charset", CHARSET);
    connection.setRequestProperty("connection", "keep-alive");
    return connection;
}

From source file:Main.java

public static String stringFromHttpPost(String urlStr, String body) {
    HttpURLConnection conn;
    try {//  w ww .j  a  va  2  s  . c  o  m
        URL e = new URL(urlStr);
        conn = (HttpURLConnection) e.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setInstanceFollowRedirects(true);
        conn.setRequestMethod("POST");
        OutputStream os1 = conn.getOutputStream();
        DataOutputStream out1 = new DataOutputStream(os1);
        out1.write(body.getBytes("UTF-8"));
        out1.flush();
        conn.connect();
        String line;
        BufferedReader reader;
        StringBuffer sb = new StringBuffer();
        if ("gzip".equals(conn.getHeaderField("Content-Encoding"))) {
            reader = new BufferedReader(
                    new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "UTF-8"));
        } else {
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        }
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
        logError(e.getMessage());
    }
    return null;
}

From source file:io.helixservice.feature.configuration.cloudconfig.CloudConfigDecrypt.java

/**
 * Decrypt an encrypted string//from   w  w  w. j  a  v a  2s. com
 * <p>
 * This method blocks on a HTTP request.
 *
 * @param name property or filename for reference/logging
 * @param encryptedValue Encrypted string
 * @param cloudConfigUri URI of the Cloud Config server
 * @param httpBasicHeader HTTP Basic header containing username and password for Cloud Config server
 * @return
 */
public static String decrypt(String name, String encryptedValue, String cloudConfigUri,
        String httpBasicHeader) {
    String result = encryptedValue;

    // Remove prefix if needed
    if (encryptedValue.startsWith(CIPHER_PREFIX)) {
        encryptedValue = encryptedValue.substring(CIPHER_PREFIX.length());
    }

    String decryptUrl = cloudConfigUri + "/decrypt";

    try {
        HttpURLConnection connection = (HttpURLConnection) new URL(decryptUrl).openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);

        connection.setRequestMethod("POST");
        connection.addRequestProperty(AUTHORIZATION_HEADER, httpBasicHeader);
        connection.setRequestProperty("Content-Type", "text/plain");
        connection.setRequestProperty("Content-Length", Integer.toString(encryptedValue.getBytes().length));
        connection.setRequestProperty("Accept", "*/*");

        // Write body
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(encryptedValue.getBytes());
        outputStream.close();

        if (connection.getResponseCode() == 200) {
            InputStream inputStream = connection.getInputStream();
            result = IOUtils.toString(inputStream);
            inputStream.close();
        } else {
            LOG.error("Unable to Decrypt name=" + name + " due to httpStatusCode="
                    + connection.getResponseCode() + " for decryptUrl=" + decryptUrl);
        }
    } catch (IOException e) {
        LOG.error("Unable to connect to Cloud Config server at decryptUrl=" + decryptUrl, e);
    }

    return result;
}

From source file:com.grosscommerce.ICEcat.utilities.Downloader.java

private static HttpURLConnection prepareConnection(String urlFrom, String login, String pwd)
        throws ProtocolException, IOException, MalformedURLException {
    URL url = new URL(urlFrom);
    HttpURLConnection uc = (HttpURLConnection) url.openConnection();
    // set up url connection to get retrieve information back
    uc.setRequestMethod("GET");
    uc.setDoInput(true);
    String val = (new StringBuffer(login).append(":").append(pwd)).toString();
    byte[] base = val.getBytes();
    String authorizationString = "Basic " + Base64.encodeBase64String(base);
    uc.setRequestProperty("Authorization", authorizationString);
    uc.setRequestProperty("Accept-Encoding", "gzip, xml");
    return uc;/*from   w ww . j  a v  a  2 s.co m*/
}

From source file:lapispaste.Main.java

private static void paster(Map<String, String> pastemap, StringBuffer pdata) {
    try {//from w w w .  ja va2 s  .  co  m
        URL url = new URL("http://paste.linux-sevenler.org/index.php");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // set connection 'writeable'
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");

        // construct the data...
        String data;
        String pdataString = pdata.toString();
        data = URLEncoder.encode("language", "ISO-8859-9") + "="
                + URLEncoder.encode(pastemap.get("format"), "ISO-8859-9");
        data += "&" + URLEncoder.encode("source", "ISO-8859-9") + "="
                + URLEncoder.encode(pdataString, "ISO-8859-9");
        data += "&" + URLEncoder.encode("submit", "ISO-8859-9") + "="
                + URLEncoder.encode(" Kaydet ", "ISO-8859-9");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // get new url where the paste is
        conn.getInputStream();
        System.out.println(conn.getURL());
        conn.disconnect();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:connector.ISConnector.java

public static JSONObject processRequest(String servlet, byte[] query) {
    JSONObject response = null;/*from   w  ww .  j a va 2 s. c o  m*/
    if (servlet != null && !servlet.startsWith("/"))
        servlet = "/" + servlet;
    try {
        // Establish HTTP connection with Identity Service
        URL url = new URL(CONTEXT_PATH + servlet);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setAllowUserInteraction(false);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        //Create the form content
        try (OutputStream out = conn.getOutputStream()) {
            out.write(query);
            out.close();
        }

        // Buffer the result into a string
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }

        rd.close();
        conn.disconnect();
        response = (JSONObject) new JSONParser().parse(sb.toString());

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

    return response;
}