Example usage for java.net URLConnection setReadTimeout

List of usage examples for java.net URLConnection setReadTimeout

Introduction

In this page you can find the example usage for java.net URLConnection setReadTimeout.

Prototype

public void setReadTimeout(int timeout) 

Source Link

Document

Sets the read timeout to a specified timeout, in milliseconds.

Usage

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Delete hero from database/*ww  w  .  j  a v  a 2 s .  c o  m*/
 * @param number    hero id 
 */
private static void deleteHero(String number) {
    try {
        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/hero/delete/" + number);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while (in.readLine() != null) {
        }
        System.out.println("Hero with id: " + number + " was deleted");
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when deleting hero");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Delete role from database//ww w .ja v  a 2  s.  c o  m
 * @param number    role id
 */
private static void deleteRole(String number) {
    try {
        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/role/delete/" + number);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while (in.readLine() != null) {
        }
        System.out.println("Role with id: " + number + " was deleted");
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when deleting role");
        System.out.println(e);
    }
}

From source file:com.android.tools.idea.sdk.remote.internal.UrlOpener.java

private static Pair<InputStream, HttpResponse> openWithUrl(String url, Header[] inHeaders) throws IOException {
    URL u = new URL(url);

    URLConnection c = u.openConnection();

    c.setConnectTimeout(sConnectionTimeoutMs);
    c.setReadTimeout(sSocketTimeoutMs);

    if (inHeaders != null) {
        for (Header header : inHeaders) {
            c.setRequestProperty(header.getName(), header.getValue());
        }//from  www. ja va  2 s  .co  m
    }

    // Trigger the access to the resource
    // (at which point setRequestProperty can't be used anymore.)
    int code = 200;

    if (c instanceof HttpURLConnection) {
        code = ((HttpURLConnection) c).getResponseCode();
    }

    // Get the input stream. That can fail for a file:// that doesn't exist
    // in which case we set the response code to 404.
    // Also we need a buffered input stream since the caller need to use is.reset().
    InputStream is = null;
    try {
        is = new BufferedInputStream(c.getInputStream());
    } catch (Exception ignore) {
        if (is == null && code == 200) {
            code = 404;
        }
    }

    HttpResponse outResponse = new BasicHttpResponse(new ProtocolVersion(u.getProtocol(), 1, 0), // make up the protocol version
            code, ""); //$NON-NLS-1$;

    Map<String, List<String>> outHeaderMap = c.getHeaderFields();

    for (Entry<String, List<String>> entry : outHeaderMap.entrySet()) {
        String name = entry.getKey();
        if (name != null) {
            List<String> values = entry.getValue();
            if (!values.isEmpty()) {
                outResponse.setHeader(name, values.get(0));
            }
        }
    }

    return Pair.of(is, outResponse);
}

From source file:com.nubits.nubot.utils.Utils.java

public static String getHTML(String url, boolean removeNonLatinChars) throws IOException {
    String line = "", all = "";
    URL myUrl = null;//  ww w. ja v a2 s  .  co m
    BufferedReader br = null;
    try {
        myUrl = new URL(url);

        URLConnection con = myUrl.openConnection();
        con.setConnectTimeout(1000 * 8);
        con.setReadTimeout(1000 * 8);
        InputStream in = con.getInputStream();

        br = new BufferedReader(new InputStreamReader(in));

        while ((line = br.readLine()) != null) {
            all += line;
        }
    } finally {
        if (br != null) {
            br.close();
        }
    }

    if (removeNonLatinChars) {
        all = all.replaceAll("[^\\x00-\\x7F]", "");
    }
    return all;
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Get specific hero with unique id//from  ww w. ja v a  2s .  c  om
 * 
 * @param number hero id 
 */
private static void getHero(String number) {
    try {
        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/hero/" + number);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String s = "";
        HeroDTO hero = new HeroDTO();
        while ((s = in.readLine()) != null) {
            hero = mapper.readValue(s, new TypeReference<HeroDTO>() {
            });
        }
        System.out.println("Returned hero");
        System.out.println("ID: " + hero.getId() + ", NAME: " + hero.getName() + ", RACE: " + hero.getRace()
                + ", XP: " + hero.getXp());
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when returning hero");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Get specific role with unique id/*  w ww  .j ava 2  s  . c o m*/
 * @param number role id
 */
private static void getRole(String number) {
    try {
        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/role/" + number);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String s = "";
        RoleDTO role = new RoleDTO();
        while ((s = in.readLine()) != null) {
            role = mapper.readValue(s, new TypeReference<RoleDTO>() {
            });
        }
        System.out.println("Returned role");
        System.out.println("ID: " + role.getId() + ", NAME: " + role.getName() + ", DESCRIPTION: "
                + role.getDescription() + ", ENERGY: " + role.getEnergy() + ", ATTACK: " + role.getAttack()
                + ", DEFENSE: " + role.getDefense());
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when returning role");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Pring all heroes to console window/*from   w ww  .  jav a 2 s  . c  om*/
 */
private static void getAllHeroes() {
    try {

        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/hero");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String s = "";
        List<HeroDTO> hero = new ArrayList<HeroDTO>();
        while ((s = in.readLine()) != null) {
            hero = mapper.readValue(s, new TypeReference<List<HeroDTO>>() {
            });
        }
        System.out.println("All heroes");
        for (HeroDTO h : hero) {
            System.out.println("ID: " + h.getId() + ", NAME: " + h.getName() + ", RACE: " + h.getRace()
                    + ", XP: " + h.getXp());
        }
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when returning all heroes");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Print list of all roles/*from ww w.  j  a  va  2s  .  co  m*/
 */
private static void getAllRoles() {
    try {
        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/role");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String s = "";
        List<RoleDTO> role = new ArrayList<RoleDTO>();
        while ((s = in.readLine()) != null) {
            role = mapper.readValue(s, new TypeReference<List<RoleDTO>>() {
            });
        }
        System.out.println("All roles");
        for (RoleDTO r : role) {
            System.out.println("ID: " + r.getId() + ", NAME: " + r.getName() + ", DESCRIPTION: "
                    + r.getDescription() + ", ENERGY: " + r.getEnergy() + ", ATTACK: " + r.getAttack()
                    + ", DEFENSE: " + r.getDefense());
        }
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when returning all roles");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Create new hero in database//from w  w w. j a va2 s. c  om
 * @param name  hero name
 * @param race  hero race
 * @param xp    hero experience
 */
private static void createHero(String name, String race, String xp) {
    try {
        HeroDTO h = new HeroDTO();
        h.setName(name);
        h.setRace(race);
        h.setXp(Integer.parseInt(xp));
        JSONObject jsonObject = new JSONObject(h);

        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/hero/post");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write(jsonObject.toString());
        out.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        while (in.readLine() != null) {
        }
        System.out.println("New hero " + h.getName() + " was created.");
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when creating new hero");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Update hero in database/*from w w w.  j av a2 s.c o  m*/
 * @param id    hero id
 * @param name  hero name
 * @param race  hero race
 * @param xp    hero experience
 */
private static void updateHero(String id, String name, String race, String xp) {
    try {
        HeroDTO h = new HeroDTO();
        h.setId(Long.parseLong(id));
        h.setName(name);
        h.setRace(race);
        h.setXp(Integer.parseInt(xp));
        JSONObject jsonObject = new JSONObject(h);

        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/hero/put");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write(jsonObject.toString());
        out.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while (in.readLine() != null) {
        }
        System.out.println("Updated hero with id: " + h.getId());
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when updating hero");
        System.out.println(e);
    }
}