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:com.meetingninja.csse.database.ContactDatabaseAdapter.java

public static List<Contact> getContacts(String userID) throws IOException {
    // Server URL setup
    String _url = getBaseUri().appendPath(userID).build().toString();
    // Establish connection
    URL url = new URL(_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    // add request header

    conn.setRequestMethod(IRequest.GET);
    addRequestHeader(conn, false);/*from   w ww . ja v a  2 s  . com*/
    // Get server response
    int responseCode = conn.getResponseCode();
    String response = getServerResponse(conn);
    List<Contact> contacts = new ArrayList<Contact>();
    List<String> contactIds = new ArrayList<String>();
    List<String> relationIds = new ArrayList<String>();
    final JsonNode contactsArray = MAPPER.readTree(response).get(Keys.User.CONTACTS);
    if (contactsArray.isArray()) {
        for (final JsonNode userNode : contactsArray) {
            relationIds.add(userNode.get(Keys.User.RELATIONID).asText());
            contactIds.add(userNode.get(Keys.User.CONTACTID).asText());
        }
    }

    conn.disconnect();
    for (int i = 0; i < contactIds.size(); i++) {

        User contact = UserDatabaseAdapter.getUserInfo(contactIds.get(i));
        if (contact != null) {
            Contact oneContact = new Contact(contact, relationIds.get(i));
            contacts.add(oneContact);
        }
    }
    return contacts;
}

From source file:com.uk_postcodes.api.Postcode.java

/**
 * Get the postcode closest to the location.
 * @param location The device's location
 * @return the closest postcode/*ww w.jav  a2 s  .c  om*/
 * @throws Exception
 */
public static String forLocation(Location location) throws Exception {
    String address = String.format(CALL, location.getLatitude(), location.getLongitude());

    Log.d("Postcode", "Calling: " + address);

    URL callUrl = new URL(address);
    HttpURLConnection callConnection = (HttpURLConnection) callUrl.openConnection();
    // The ten-second rule:
    // If there's no data in 10s (or TIMEOUT), assume the worst.
    callConnection.setReadTimeout(10000);
    // Set the request method to GET.
    callConnection.setRequestMethod("GET");
    int code = callConnection.getResponseCode();
    if (code != HttpURLConnection.HTTP_OK) {
        throw new Exception("A non-200 code was returned: " + code);
    }

    String responseStr;
    responseStr = IOUtils.toString(callConnection.getInputStream());
    callConnection.disconnect();

    JsonParser jp = new JsonParser();
    JsonElement response = jp.parse(responseStr);
    return response.getAsJsonObject().get("postcode").getAsString();
}

From source file:com.meetingninja.csse.database.AgendaDatabaseAdapter.java

public static Agenda getAgenda(String agendaID) throws IOException {
    // Server URL setup
    String _url = getBaseUri().appendPath(agendaID).build().toString();

    // Establish connection
    URL url = new URL(_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    // add request header
    conn.setRequestMethod(IRequest.GET);
    addRequestHeader(conn, false);//w w  w . ja va  2 s  . c  o  m

    // Get server response
    int responseCode = conn.getResponseCode();
    String response = getServerResponse(conn);
    JsonNode agendaNode = MAPPER.readTree(response);

    return parseAgenda(agendaNode);
}

From source file:fr.lissi.belilif.om2m.rest.WebServiceActions.java

/**
 * Checks if a resource is reachable.//from  ww  w.  j a  v  a  2s . com
 *
 * @param uri the uri
 * @return true, if is reachable
 */
public static boolean isReachable(String uri) {
    try {
        HttpURLConnection.setFollowRedirects(false);
        HttpURLConnection myURLConnection = (HttpURLConnection) new URL(uri).openConnection();
        myURLConnection.setRequestMethod("HEAD");
        return (myURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.zimbra.common.util.ngxlookup.ZimbraNginxLookUpClient.java

/**
 * Pings a HTTP(S) URL. This effectively sends a GET request and returns <code>true</code> if the response code is 200
 * @param url The HTTP(S) URL to be pinged.
 * @param timeout The timeout in millis for both the connection timeout.
 * @return <code>true</code> if the given HTTP(S) URL has returned response code 200 within the
 * given timeout, otherwise <code>false</code>.
 *//*www . j  a va  2 s .  c  o m*/
public static boolean ping(String url, int timeout) {
    ZimbraLog.misc.debug("attempting to ping \"%s\" with timeout %d", url, timeout);
    try {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setConnectTimeout(timeout);
        return (connection.getResponseCode() == 200) ? true : false;
    } catch (IOException exception) {
        return false;
    }
}

From source file:Main.java

public static String getResponse(String urlParam) {
    try {/* w  ww.  j  a  v a 2  s  .  c  o  m*/
        URL url = new URL(urlParam);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE6.0; Windows NT 5.1; SV1)");
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = conn.getInputStream();
            return inputStream2String(inputStream);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

From source file:editor.util.URLUtil.java

public static String sendPost(String url, String data) throws Exception {

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");

    // Send post request
    con.setDoOutput(true);/*from  w w w  .j ava 2s.c om*/
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(data);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

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

From source file:Main.java

static InputStream openRemoteInputStream(Uri uri) {
    URL finalUrl;/*from ww  w.j a va  2 s  .c o m*/
    try {
        finalUrl = new URL(uri.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    }
    HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) finalUrl.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    connection.setInstanceFollowRedirects(false);
    int code;
    try {
        code = connection.getResponseCode();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    if ((code == 301) || (code == 302) || (code == 303)) {
        String newLocation = connection.getHeaderField("Location");
        return openRemoteInputStream(Uri.parse(newLocation));
    }
    try {
        return (InputStream) finalUrl.getContent();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

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

    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.telefonica.iot.perseo.test.Help.java

public static Res sendMethod(String url, String body, String method) throws Exception {
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod(method);/*w w  w .  j  a v  a2  s.  c o  m*/
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(body);
    wr.flush();
    wr.close();
    int responseCode = con.getResponseCode();
    String responseBody = getBodyResponse(con);
    return new Res(responseCode, responseBody);
}