List of usage examples for java.net URLConnection setDoOutput
public void setDoOutput(boolean dooutput)
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 . com * * @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/*from w ww . 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.pa165.ddtroops.console.client.Application.java
/** * Pring all heroes to console window/*from ww w .j a v a 2s . co 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 w w .j a va2 s . 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 ww w. j a v a 2s. 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 . java 2s .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/*w w w . j a v a2 s. c o m*/ * @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); } }
From source file:com.pa165.ddtroops.console.client.Application.java
/** * Update role in database/* www . j a va2s. co m*/ * @param id role id * @param name role name * @param description role desc * @param energy role energy * @param attack role attack * @param defense role defense */ private static void updateRole(String id, String name, String description, String energy, String attack, String defense) { try { RoleDTO r = new RoleDTO(); r.setId(Long.parseLong(id)); 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/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 role with id: " + r.getId()); System.out.println(); in.close(); } catch (Exception e) { System.out.println("\nError when updating role"); System.out.println(e); } }
From source file:com.buglabs.dragonfly.util.WSHelper.java
/** * @param url//w w w . ja v a 2 s. c om * @param inputStream * @return * @throws IOException */ protected static String post(URL url, InputStream inputStream) throws IOException { URLConnection connection = url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); OutputStream stream = connection.getOutputStream(); writeTo(stream, inputStream); stream.flush(); stream.close(); InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line, resp = new String(""); while ((line = rd.readLine()) != null) { resp = resp + line + "\n"; } rd.close(); is.close(); return resp; }
From source file:org.apache.niolex.commons.net.DownloadUtil.java
/** * Download the file pointed by the url and return the content as byte array. * * @param strUrl/*w w w .ja va 2s.co m*/ * The Url to be downloaded. * @param connectTimeout * Connect timeout in milliseconds. * @param readTimeout * Read timeout in milliseconds. * @param maxFileSize * Max file size in BYTE. * @param useCache Whether we use thread local cache or not. * @return The file content as byte array. * @throws NetException */ public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize, Boolean useCache) throws NetException { LOG.debug("Start to download file [{}], C{}R{}M{}.", strUrl, connectTimeout, readTimeout, maxFileSize); InputStream in = null; try { URL url = new URL(strUrl); // We use Java URL to download file. URLConnection ucon = url.openConnection(); ucon.setConnectTimeout(connectTimeout); ucon.setReadTimeout(readTimeout); ucon.setDoOutput(false); ucon.setDoInput(true); ucon.connect(); final int contentLength = ucon.getContentLength(); validateContentLength(strUrl, contentLength, maxFileSize); if (ucon instanceof HttpURLConnection) { validateHttpCode(strUrl, (HttpURLConnection) ucon); } in = ucon.getInputStream(); // Get the input stream. byte[] ret = null; // Create the byte array buffer according to the strategy. if (contentLength > 0) { ret = commonDownload(contentLength, in); } else { ret = unusualDownload(strUrl, in, maxFileSize, useCache); } LOG.debug("Succeeded to download file [{}] size {}.", strUrl, ret.length); return ret; } catch (NetException e) { LOG.info(e.getMessage()); throw e; } catch (Exception e) { String msg = "Failed to download file " + strUrl + " msg=" + e.toString(); LOG.warn(msg); throw new NetException(NetException.ExCode.IOEXCEPTION, msg, e); } finally { // Close the input stream. StreamUtil.closeStream(in); } }