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

/** 
 * Get image from newwork //from  w w  w.ja v a2  s.  com
 * @param path The path of image 
 * @return InputStream 
 * @throws Exception 
 */
public static InputStream getImageStream(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5 * 1000);
    conn.setRequestMethod("GET");
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return conn.getInputStream();
    }
    return null;
}

From source file:Main.java

private static InputStream OpenHttpConnection(String strURL, Boolean cache) throws IOException {
    InputStream inputStream = null;

    try {/* w ww.j a  va2 s  .  c o m*/
        URL url = new URL(strURL);
        URLConnection conn = url.openConnection();
        conn.setUseCaches(cache);
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
            inputStream = httpConn.getInputStream();

    } catch (Exception ex) {
    }
    return inputStream;
}

From source file:Main.java

public static Bitmap getImage(String urlpath) throws Exception {
    Bitmap bitmap = null;/*  www  .j av a 2s .  c o m*/
    URL url = new URL(urlpath);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5 * 1000);
    if (conn.getResponseCode() == 200) {
        InputStream inputStream = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(inputStream);
    }
    return bitmap;
}

From source file:Main.java

@SuppressLint("NewApi")
public static void getURL(String path) {
    String fileName = "";
    String dir = "/IndoorNavi/";
    File sdRoot = Environment.getExternalStorageDirectory();
    try {/*  w  ww .  j a v  a 2  s. c o m*/
        // Open the URLConnection for reading
        URL u = new URL(path);
        // URL u = new URL("http://www.baidu.com/");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();

        int code = uc.getResponseCode();
        String response = uc.getResponseMessage();
        //System.out.println("HTTP/1.x " + code + " " + response);
        for (int j = 1;; j++) {
            String key = uc.getHeaderFieldKey(j);
            String header = uc.getHeaderField(j);
            if (!(key == null)) {
                if (key.equals("Content-Name"))
                    fileName = header;
            }
            if (header == null || key == null)
                break;
            //System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
        }
        Log.i("zhr", fileName);
        //System.out.println();

        try (InputStream in = new BufferedInputStream(uc.getInputStream())) {

            // chain the InputStream to a Reader
            Reader r = new InputStreamReader(in);
            int c;
            File mapFile = new File(sdRoot, dir + fileName);
            mapFile.createNewFile();
            FileOutputStream filecon = new FileOutputStream(mapFile);
            while ((c = r.read()) != -1) {
                //System.out.print((char) c);
                filecon.write(c);
                filecon.flush();

            }
            filecon.close();
        }

    } catch (MalformedURLException ex) {
        System.err.println(path + " is not a parseable URL");
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:Main.java

public static boolean isExists(String urlString) {
    try {/*  w ww  . j a  v a  2 s.  com*/
        URL u = new URL(urlString);
        HttpURLConnection huc = (HttpURLConnection) u.openConnection();
        huc.setRequestMethod("GET");
        huc.connect();
        int rc = huc.getResponseCode();
        return (rc == HttpURLConnection.HTTP_OK);
        // Handle response code here...
    } catch (UnknownHostException uhe) {
        // Handle exceptions as necessary
        Log.w("EPub", uhe.getMessage());
    } catch (FileNotFoundException fnfe) {
        // Handle exceptions as necessary
        Log.w("EPub", fnfe.getMessage());
    } catch (Exception e) {
        // Handle exceptions as necessary
        Log.w("EPub", e.getMessage());
    }
    return false;
}

From source file:com.google.cloud.runtime.jetty.util.HttpUrlUtil.java

/**
 * Wait for a server to be "up" by requesting a specific GET resource
 * that should be returned in status code 200.
 * <p>//w w w.j av  a 2  s .c o m
 * This will attempt a check for server up.
 * If any result other then response code 200 occurs, then
 * a 2s delay is performed until the next test.
 * Up to the duration/timeunit specified.
 * </p>
 *
 * @param uri      the URI to request
 * @param duration the time duration to wait for server up
 * @param unit     the time unit to wait for server up
 */
public static void waitForServerUp(URI uri, int duration, TimeUnit unit) {
    System.err.println("Waiting for server up: " + uri);
    boolean waiting = true;
    long expiration = System.currentTimeMillis() + unit.toMillis(duration);
    while (waiting && System.currentTimeMillis() < expiration) {
        try {
            System.out.print(".");
            HttpURLConnection http = openTo(uri);
            int statusCode = http.getResponseCode();
            if (statusCode != HttpURLConnection.HTTP_OK) {
                log.log(Level.FINER, "Waiting 2s for next attempt");
                TimeUnit.SECONDS.sleep(2);
            } else {
                waiting = false;
            }
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Invalid URI: " + uri.toString());
        } catch (IOException e) {
            log.log(Level.FINEST, "Ignoring IOException", e);
        } catch (InterruptedException ignore) {
            // ignore
        }
    }
    System.err.println();
    System.err.println("Server seems to be up.");
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {/*from   www  .j  a v  a2 s.c  om*/
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(8000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {/*from  ww  w  .j a v a2  s  .  c  om*/
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(10000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

From source file:dev.meng.wikidata.util.http.HttpUtils.java

public static JSONObject queryForJSONResponse(URL url, String encoding)
        throws ProtocolException, IOException, StringConvertionException {
    JSONObject response = null;/*from w  w w.j  av a2s .c om*/

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.connect();

    if (connection.getResponseCode() == 200) {
        response = new JSONObject(StringUtils.inputStreamToString(connection.getInputStream(), encoding));
    } else {
        throw new IOException("Error in opening: " + url + ", " + connection.getResponseCode() + " "
                + connection.getResponseMessage());
    }

    return response;
}

From source file:org.elegosproject.romupdater.DownloadManager.java

public static boolean checkHttpFile(String url) {
    try {/*from   w w w . j  a v  a  2 s  . c o  m*/
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
        Log.i(TAG, "Testing " + url + "...");
        URL theUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) theUrl.openConnection();
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            connection.disconnect();
        } else {
            Log.i(TAG, "HTTP Response code: " + connection.getResponseCode());
            return false;
        }
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        return false;
    }
    return true;
}