List of usage examples for java.net URLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:com.pa165.ddtroops.console.client.Application.java
/** * Get specific hero with unique id/*w w w . j a v a 2 s .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:org.wso2.carbon.cloud.gateway.agent.CGAgentUtils.java
public static OMNode getOMElementFromURI(String wsdlURI) throws CGException { if (wsdlURI == null || "null".equals(wsdlURI)) { throw new CGException("Can't create URI from a null value"); }//www . jav a 2 s . co m URL url; try { url = new URL(wsdlURI); } catch (MalformedURLException e) { throw new CGException("Invalid URI reference '" + wsdlURI + "'", e); } URLConnection connection; connection = getURLConnection(url); if (connection == null) { throw new CGException("Cannot create a URLConnection for given URL : " + url); } connection.setReadTimeout(getReadTimeout()); connection.setConnectTimeout(getConnectTimeout()); connection.setRequestProperty("Connection", "close"); // if http is being used InputStream inStream = null; try { inStream = connection.getInputStream(); StAXOMBuilder builder = new StAXOMBuilder(inStream); OMElement doc = builder.getDocumentElement(); doc.build(); return doc; } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("Reading as XML failed due to ", e); } return readNonXML(url); } finally { try { if (inStream != null) { inStream.close(); } } catch (IOException e) { log.warn("Error while closing the input stream to: " + url, e); } } }
From source file:com.pa165.ddtroops.console.client.Application.java
/** * Get specific role with unique id// www . j av a2 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.nubits.nubot.utils.Utils.java
public static String getHTML(String url, boolean removeNonLatinChars) throws IOException { String line = "", all = ""; URL myUrl = null;//from w ww . ja v a 2s. 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.dynamobi.network.DynamoNetworkUdr.java
/** * Fetches the master metadata.json file on a given repository and returns the * data as a JSONObject./*from w ww. j av a2s . c om*/ */ private static JSONObject downloadMetadata(String repo) throws SQLException { if (repo == null) return null; try { // Grab master metadata.json URL u = new URL(repo + "/metadata.json"); URLConnection uc = u.openConnection(); uc.setConnectTimeout(1000); // generous max-of-1-second to connect uc.setReadTimeout(1000); uc.connect(); InputStreamReader in = new InputStreamReader(uc.getInputStream()); BufferedReader buff = new BufferedReader(in); StringBuffer sb = new StringBuffer(); String line = null; do { line = buff.readLine(); if (line != null) sb.append(line); } while (line != null); String data = sb.toString(); // Parse it JSONParser parser = new JSONParser(); JSONObject ob = (JSONObject) parser.parse(data); return ob; } catch (SocketTimeoutException e) { throw new SQLException(URL_TIMEOUT); } catch (MalformedURLException e) { throw new SQLException("Bad URL."); } catch (IOException e) { throw new SQLException(URL_TIMEOUT); } catch (ParseException e) { throw new SQLException("Could not parse data from URL."); } }
From source file:com.pa165.ddtroops.console.client.Application.java
/** * Pring all heroes to console window/* w w w .ja v a 2 s.c o m*/ */ 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// w ww.j a va2s.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 av a2 s . c o m*/ * @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// ww w . j a va 2 s . co 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); } }
From source file:com.pa165.ddtroops.console.client.Application.java
/** * Create new role in database//from w w w . j av a 2 s . com * @param name role id * @param description role desc * @param energy role energy * @param attack role attack * @param defense role defense */ private static void createRole(String name, String description, String energy, String attack, String defense) { try { RoleDTO r = new RoleDTO(); r.setName(name); r.setDescription(description); r.setEnergy(Integer.parseInt(energy)); r.setAttack(Integer.parseInt(attack)); r.setDefense(Integer.parseInt(defense)); JSONObject jsonObject = new JSONObject(r); URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/role/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 role " + r.getName() + " was created."); System.out.println(); in.close(); } catch (Exception e) { System.out.println("\nError when creating new role"); System.out.println(e); } }