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

static String downloadUrl(String myurl) throws IOException {
    InputStream is = null;//from  w ww.j a v a  2s  .c  om

    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", "Basic cm9vdDpvcmllbnRkYg==");
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        //start
        conn.connect();
        int response = conn.getResponseCode();
        Log.e("The response is: ", "" + response);
        is = conn.getInputStream();
        //converte inputStream in stringa
        String contentAsString = readIt(is);
        return contentAsString;
    } catch (IOException e) {
        Log.e("HTTP", e.getMessage());
        return null;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:com.rumblefish.friendlymusic.api.WebRequest.java

public static Bitmap getBitmapAtURL(URL url) {
    InputStream inStream = null;/*w ww  .  j a  v  a 2 s.c  om*/
    HttpURLConnection _conn = null;
    Bitmap bitmap = null;
    try {

        _conn = (HttpURLConnection) url.openConnection();
        _conn.setDoInput(true);
        _conn.connect();
        inStream = _conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(inStream);
        inStream.close();
        _conn.disconnect();
        inStream = null;
        _conn = null;
    } catch (Exception ex) {
        // nothing
    }
    if (inStream != null) {
        try {
            inStream.close();
        } catch (Exception ex) {
        }
    }
    if (_conn != null) {
        _conn.disconnect();
    }

    return bitmap;
}

From source file:com.cnaude.mutemanager.UUIDFetcher.java

private static HttpURLConnection createConnection() throws Exception {
    URL url = new URL(PROFILE_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);/*  ww  w.j  av a  2  s  .c  om*/
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}

From source file:Main.java

/**
 * Do an HTTP POST and return the data as a byte array.
 *//*from  ww w. j  av a 2 s  .c o  m*/
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws MalformedURLException, IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        urlConnection.connect();
        if (data != null) {
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(data);
            out.close();
        }
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        return convertInputStreamToByteArray(in);
    } catch (IOException e) {
        String details;
        if (urlConnection != null) {
            details = "; code=" + urlConnection.getResponseCode() + " (" + urlConnection.getResponseMessage()
                    + ")";
        } else {
            details = "";
        }
        Log.e("ExoplayerUtil", "executePost: Request failed" + details, e);
        throw e;
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:Main.java

/**
 * Executes a post request using {@link HttpURLConnection}.
 *
 * @param url The request URL.//from  w w  w  . j  a v a 2s.  co m
 * @param data The request body, or null.
 * @param requestProperties Request properties, or null.
 * @return The response body.
 * @throws IOException If an error occurred making the request.
 */
// TODO: Remove this and use HttpDataSource once DataSpec supports inclusion of a POST body.
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        // Write the request body, if there is one.
        if (data != null) {
            OutputStream out = urlConnection.getOutputStream();
            try {
                out.write(data);
            } finally {
                out.close();
            }
        }
        // Read and return the response body.
        InputStream inputStream = urlConnection.getInputStream();
        try {
            return toByteArray(inputStream);
        } finally {
            inputStream.close();
        }
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:com.mk4droid.IMC_Services.Download_Data.java

/**
 * Download Image from a certain url/*from w  w w.  ja  va  2  s. com*/
 * 
 * @param fullPath the url of the image
 * @return
 */
public static byte[] Down_Image(String fullPath) {

    try {
        //----- Split----
        String[] AllInfo = fullPath.split("/");

        // Encode filename as UTF8 -------
        String fnExt = AllInfo[AllInfo.length - 1];

        String fnExt_UTF8 = URLEncoder.encode(fnExt, "UTF-8");

        //- Replace new fn to old
        AllInfo[AllInfo.length - 1] = fnExt_UTF8;

        //------ Concatenate to a single string -----
        String newfullPath = AllInfo[0];
        for (int i = 1; i < AllInfo.length; i++)
            newfullPath += "/" + AllInfo[i];

        // empty space becomes + after UTF8, then replace with %20
        newfullPath = newfullPath.replace("+", "%20");

        //------------ Download -------------
        URL myFileUrl = new URL(newfullPath);

        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.setConnectTimeout(10000);
        conn.connect();
        InputStream isBitmap = conn.getInputStream();
        return readBytes(isBitmap);

    } catch (Exception e) {
        Log.e(Constants_API.TAG, "Download_Data: Down_Image: Error in http connection " + e.getMessage());
        return null;
    }

}

From source file:com.intellectualcrafters.plot.uuid.UUIDFetcher.java

private static HttpURLConnection createConnection() throws Exception {
    final URL url = new URL(PROFILE_URL);
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);/*from   ww w  . j  av a 2s. c o  m*/
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}

From source file:com.surfs.storage.common.util.HttpUtils.java

public static String invokeHttpForGet(String path, String... agrs) throws IOException {
    URL url = new URL(path);
    LogFactory.info("rest url:" + url.toString());
    HttpURLConnection con = null;
    try {// w  ww. j  ava  2s.c o m
        con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setConnectTimeout(10000);
        con.setReadTimeout(1000 * 60 * 30);
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setUseCaches(false);

        for (String string : agrs) {
            con.setRequestProperty("Content-type", "application/json");
            con.setRequestMethod("POST");
            OutputStream out = con.getOutputStream();
            out.write(string.getBytes("UTF-8"));
        }

        if (con.getResponseCode() != 200)
            throw new ConnectException(con.getResponseMessage());

        InputStream is = con.getInputStream();

        return readResponse(is);

    } catch (IOException e) {
        throw e;
    } finally {
        if (con != null) {
            con.disconnect();
        }
    }
}

From source file:com.google.api.ads.adwords.awreporting.server.appengine.exporter.ReportExporterAppEngine.java

public static void html2PdfOverNet(InputStream htmlSource, ReportWriter reportWriter) {
    try {//from w ww.  j  av a 2  s . co m
        URL url = new URL(HTML_2_PDF_SERVER_URL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/html");
        connection.setRequestProperty("charset", "utf-8");
        connection.setUseCaches(false);

        DataOutputStream send = new DataOutputStream(connection.getOutputStream());
        send.write(IOUtils.toByteArray(htmlSource));
        send.flush();

        // Read from connection
        reportWriter.write(connection.getInputStream());

        send.close();

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

From source file:core.RESTCalls.RESTPost.java

public static String httpPost(String urlStr, String[] paramName, String[] paramVal) throws Exception {

    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");

    conn.setDoOutput(true);//  ww  w .  ja v  a 2s. co  m
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(false);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    OutputStream out = conn.getOutputStream();

    Writer writer = new OutputStreamWriter(out, "UTF-8");

    for (int i = 0; i < paramName.length; i++) {

        writer.write(paramName[i]);
        writer.write("=");
        writer.write(URLEncoder.encode(paramVal[i], "UTF-8"));
        writer.write("&");
    }

    writer.close();
    out.close();

    if (conn.getResponseCode() != 200)
        throw new IOException(conn.getResponseMessage());

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    StringBuilder sb = new StringBuilder();

    String line;

    while ((line = rd.readLine()) != null)
        sb.append(line + "\n");

    rd.close();

    conn.disconnect();

    return sb.toString();
}