Example usage for java.net HttpURLConnection getResponseCode

List of usage examples for java.net HttpURLConnection getResponseCode

Introduction

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

Prototype

public int getResponseCode() throws IOException 

Source Link

Document

Gets the status code from an HTTP response message.

Usage

From source file:Main.java

private static InputStream getHttpConnection(String urlString) throws IOException {
    InputStream stream = null;/*  w w  w. j a  va 2  s  .c  om*/
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return stream;
}

From source file:Main.java

/**
 * Makes HttpURLConnection and returns InputStream
 *//*from  w  w  w .j a va  2  s. c  om*/
private static InputStream getHttpConnection(String urlString) throws IOException {
    InputStream stream = null;
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return stream;
}

From source file:net.ftb.data.news.RSSReader.java

public static List<NewsArticle> readRSS() {
    try {//from w  ww.ja  v a 2 s  .  c  o  m
        List<NewsArticle> news = Lists.newArrayList();

        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        URL u = new URL(Locations.feedURL);
        HttpURLConnection conn = (HttpURLConnection) u.openConnection();
        conn.connect();
        if (conn.getResponseCode() != 200) {
            Logger.logWarn("News download failed");
            AppUtils.debugConnection(conn);
            conn.disconnect();
            return null;
        }
        Document doc = builder.parse(conn.getInputStream());
        NodeList nodes = doc.getElementsByTagName("item");

        for (int i = 0; i < nodes.getLength(); i++) {
            Element element = (Element) nodes.item(i);
            NewsArticle article = new NewsArticle();
            article.setTitle(getTextValue(element, "title"));
            article.setHyperlink(getTextValue(element, "link"));
            article.setBody(getTextValue(element, "content:encoded"));
            article.setDate(getTextValue(element, "pubDate"));
            news.add(article);
        }

        return news;
    } catch (Exception ex) {
        Logger.logWarn("News download failed", ex);
        return null;
    }
}

From source file:Main.java

public static byte[] downloadImage(final URL url) {
    InputStream in = null;//from w ww .j ava2 s . co  m
    int responseCode = -1;

    try {
        final HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.connect();
        responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            in = con.getInputStream();
            return getByteFromInputStream(in);
        }
    } catch (final IOException e) {
        Log.e(LOG_TAG, "Failed to download image from: " + url.toString(), e);
    }
    return null;
}

From source file:Main.java

public static InputStream getHTTPInputStream(String url) {
    try {/*from w  ww  . j  av a 2s . c o m*/
        URL urlObj = null;
        urlObj = new URL(url);

        HttpURLConnection urlConnection = null;

        urlConnection = (HttpURLConnection) urlObj.openConnection();

        urlConnection.connect();

        int response = urlConnection.getResponseCode();

        if (response != 200) {
            return null;
        }

        return urlConnection.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getImageFromURL(URL url) {

    Bitmap bitmap = null;/*from   ww  w . j av a  2 s. c  o  m*/

    HttpURLConnection connection = null;

    try {

        connection = (HttpURLConnection) url.openConnection();

        if (connection.getResponseCode() != 200) {
            return null;
        }
        if (!CONTENT_TYPE_IMAGE.equalsIgnoreCase(connection.getContentType().substring(0, 5))) {
            return null;
        }

        bitmap = BitmapFactory.decodeStream(connection.getInputStream());

    } catch (IOException e) {

        e.printStackTrace();

    }

    if (connection != null) {

        connection.disconnect();

    }

    return bitmap;

}

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

public static void download(String urlFrom, String login, String pwd, OutputStream destStream)
        throws Exception {
    try {/*from   w ww  .ja  va 2 s  .co m*/
        HttpURLConnection uc = prepareConnection(urlFrom, login, pwd);

        uc.connect();

        if (uc.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Error, code: {0}, message {1}",
                    new Object[] { uc.getResponseCode(), uc.getResponseMessage() });

            return;
        }

        BufferedInputStream is = null;

        if ((uc.getContentEncoding() != null && uc.getContentEncoding().toLowerCase().equals("gzip"))
                || uc.getContentType() != null && uc.getContentType().toLowerCase().contains("gzip")) {
            is = new BufferedInputStream(new GZIPInputStream(uc.getInputStream()));

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Will download gzip data from: {0}",
                    urlFrom);
        } else {
            is = new BufferedInputStream(uc.getInputStream());

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO,
                    "Will download not compressed data from:{0}", urlFrom);
        }

        StreamsHelper.copy(is, destStream);
        destStream.flush();

    } catch (Exception ex) {
        Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, "URL: " + urlFrom, ex);

        throw ex;
    }
}

From source file:com.telefonica.iot.perseo.test.Help.java

private static String getBodyResponse(HttpURLConnection con) throws IOException {
    int responseCode = con.getResponseCode();
    InputStream stream;/*  w  w  w .j  a v  a  2s.  co m*/
    if (responseCode / 100 == 2) {
        stream = con.getInputStream();
    } else {
        stream = con.getErrorStream();
    }
    if (stream != null) {
        BufferedReader in = new BufferedReader(new InputStreamReader(stream));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    }
    return null;
}

From source file:Main.java

public static boolean isURLConnectable(Context context, String url) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {// ww w .  ja v a 2  s .co  m
            HttpURLConnection urlc = (HttpURLConnection) new URL(url).openConnection();
            urlc.setConnectTimeout(10 * 1000); // 10 s.
            urlc.connect();
            if (urlc.getResponseCode() == 200) { // 200 = "OK" code (http
                // connection is fine).
                Log.wtf("Connection", "Success !");
                return true;
            } else {
                return false;
            }
        } catch (MalformedURLException e1) {
            return false;
        } catch (IOException e) {
            return false;
        }
    }
    return false;
}

From source file:com.taobao.diamond.server.controller.CheckController.java

public static String url2str(String sURL, String encoding) {
    try {//from  w  w  w .j  a va2 s. c  om
        URL url = new URL(sURL);
        URLConnection conn = url.openConnection();
        if (conn instanceof HttpURLConnection) {
            HttpURLConnection httpUrl = (HttpURLConnection) conn;
            httpUrl.getResponseCode();
        }
        InputStream is = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding));
        String r = "";
        char[] cbuf = new char[1024];
        while (br.read(cbuf) > 0) {
            r += new String(cbuf);
        }
        return r;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}