List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:com.pa165.ddtroops.console.client.Application.java
/** * Print list of all roles/*from w w w . j a v a 2 s. c o 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 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/*from www.j a v 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); } }
From source file:Downloader.java
/** * Creates an URL connection for the specified URL and data. * //from w w w .java2 s . co m * @param url The URL to connect to * @param postData The POST data to pass to the URL * @return An URLConnection for the specified URL/data * @throws java.net.MalformedURLException If the specified URL is malformed * @throws java.io.IOException If an I/O exception occurs while connecting */ private static URLConnection getConnection(final String url, final String postData) throws MalformedURLException, IOException { final URL myUrl = new URL(url); final URLConnection urlConn = myUrl.openConnection(); urlConn.setUseCaches(false); urlConn.setDoInput(true); urlConn.setDoOutput(postData.length() > 0); urlConn.setConnectTimeout(10000); if (postData.length() > 0) { urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); final DataOutputStream out = new DataOutputStream(urlConn.getOutputStream()); out.writeBytes(postData); out.flush(); out.close(); } return urlConn; }
From source file:com.pa165.ddtroops.console.client.Application.java
/** * Create new role in database// w ww .ja v a 2 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/* w w w . j av a 2 s .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.agc.tmdb.Util.URLFetcher.java
/** * Used to set the Browser Properties.//from www. j ava 2s .c o m * @param URLConnection Object. */ private static void setBrowserProperties(URLConnection connectionObject) { if (browserProperties.isEmpty()) { browserProperties.put("User-Agent", "Mozilla/5.25 Netscape/5.0 (Windows; I; Win95)"); } for (Map.Entry<String, String> browserProperty : browserProperties.entrySet()) { connectionObject.setRequestProperty(browserProperty.getKey(), browserProperty.getValue()); } }
From source file:com.gallatinsystems.common.util.S3Util.java
public static URLConnection getConnection(String bucketName, String objectKey, String awsAccessKeyId, String awsAccessSecret) throws IOException { final String date = getDate(); final String payload = String.format(GET_PAYLOAD, date, bucketName, objectKey); final String signature = MD5Util.generateHMAC(payload, awsAccessSecret); final URL url = new URL(String.format(S3_URL, bucketName, objectKey)); final URLConnection conn = url.openConnection(); conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setReadTimeout(CONNECTION_TIMEOUT); conn.addRequestProperty("Cache-Control", "no-cache,max-age=0"); conn.setRequestProperty("Date", date); conn.setRequestProperty("Authorization", "AWS " + awsAccessKeyId + ":" + signature); return conn;// w w w. ja v a 2 s .c o m }
From source file:org.wso2.carbon.connector.common.ConnectorIntegrationUtil.java
public static int sendRequestToRetriveHeaders(String addUrl, String query, String contentType) throws IOException, JSONException { String charset = "UTF-8"; URLConnection connection = new URL(addUrl).openConnection(); connection.setDoOutput(true);//from w w w. j a v a2 s .c o m connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", contentType + ";charset=" + charset); OutputStream output = null; try { output = connection.getOutputStream(); output.write(query.getBytes(charset)); } finally { if (output != null) { try { output.close(); } catch (IOException logOrIgnore) { log.error("Error while closing the connection"); } } } HttpURLConnection httpConn = (HttpURLConnection) connection; int responseCode = httpConn.getResponseCode(); return responseCode; }
From source file:org.wso2.carbon.connector.common.ConnectorIntegrationUtil.java
public static int sendRequestToRetriveHeaders(String addUrl, String query) throws IOException, JSONException { String charset = "UTF-8"; URLConnection connection = new URL(addUrl).openConnection(); connection.setDoOutput(true);/*w w w . jav a 2 s.c o m*/ connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", "application/json;charset=" + charset); OutputStream output = null; try { output = connection.getOutputStream(); output.write(query.getBytes(charset)); } finally { if (output != null) { try { output.close(); } catch (IOException logOrIgnore) { log.error("Error while closing the connection"); } } } HttpURLConnection httpConn = (HttpURLConnection) connection; int responseCode = httpConn.getResponseCode(); return responseCode; }