List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:com.moviejukebox.themoviedb.tools.WebBrowser.java
private static void sendHeader(URLConnection cnx) { populateBrowserProperties();//from w ww . ja v a 2 s. c om // send browser properties for (Map.Entry<String, String> browserProperty : browserProperties.entrySet()) { cnx.setRequestProperty(browserProperty.getKey(), browserProperty.getValue()); } // send cookies String cookieHeader = createCookieHeader(cnx); if (!cookieHeader.isEmpty()) { cnx.setRequestProperty("Cookie", cookieHeader); } }
From source file:org.wso2.carbon.connector.integration.test.common.ConnectorIntegrationUtil.java
public static JSONObject sendRequest(String addUrl, String query) throws IOException, JSONException { String charset = "UTF-8"; URLConnection connection = new URL(addUrl).openConnection(); connection.setDoOutput(true);//ww w . j a v a 2 s . com 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; InputStream response; if (httpConn.getResponseCode() >= 400) { response = httpConn.getErrorStream(); } else { response = connection.getInputStream(); } String out = "{}"; if (response != null) { StringBuilder sb = new StringBuilder(); byte[] bytes = new byte[1024]; int len; while ((len = response.read(bytes)) != -1) { sb.append(new String(bytes, 0, len)); } if (!sb.toString().trim().isEmpty()) { out = sb.toString(); } } JSONObject jsonObject = new JSONObject(out); return jsonObject; }
From source file:UploadUtils.ImgurUpload.java
/** * Connect to image host.//from w w w . j a va2s. c o m * @return the connection * @throws java.MalformedURLExceptionMalformedURLException if the URL can't be constructed successfully * @throws java.IOException if failed to open connection to the newly constructed URL */ private static URLConnection connect() throws MalformedURLException, IOException { URLConnection conn = null; try { // opens connection and sends data URL url = new URL(IMGUR_POST_URI); conn = url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("Authorization", "Client-ID " + IMGUR_API_KEY); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } catch (MalformedURLException ex) { throw ex; } catch (IOException ex) { throw ex; } return conn; }
From source file:com.pa165.ddtroops.console.client.Application.java
/** * Delete hero from database/* w ww . j a v a 2 s . co 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/*w w w. ja v a 2 s .c om*/ * @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:de.l3s.boilerpipe.sax.HTMLFetcher.java
public static HTMLDocument fetchHelper(final URL url) throws IOException { final URLConnection conn = url.openConnection(); //conn.setRequestProperty("User-Agent", //"Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36"); //conn.setRequestProperty("Cookie","wapparam=web2wap; vt=4"); final String ct = conn.getContentType(); if (ct == null || !(ct.equals("text/html") || ct.startsWith("text/html;"))) { //throw new IOException("Unsupported content type: "+ct+ url); System.err.println("WARN: unsupported Content-type: " + ct + url); }//w w w.j av a 2 s. co m Charset cs = Charset.forName("UTF8"); if (ct != null) { Matcher m = PAT_CHARSET_REX.matcher(ct); if (m.find()) { final String charset = m.group(1); try { cs = Charset.forName(charset); } catch (UnsupportedCharsetException e) { // keep default } } } InputStream in = conn.getInputStream(); final String encoding = conn.getContentEncoding(); if (encoding != null) { if ("gzip".equalsIgnoreCase(encoding)) { in = new GZIPInputStream(in); } else { System.err.println("WARN: unsupported Content-Encoding: " + encoding); } } ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[4096]; int r; while ((r = in.read(buf)) != -1) { bos.write(buf, 0, r); } in.close(); final byte[] data = bos.toByteArray(); return new HTMLDocument(data, cs); }
From source file:org.royaldev.royalcommands.VUUpdater.java
/** * Gets the update information from the title of the latest file available from the CurseForge API. * * @param pluginID ID of the plugin to get the information from. The ID comes from CurseForge. * @return {@link org.royaldev.royalcommands.VUUpdater.VUUpdateInfo} * @throws IOException If any errors occur *///from w w w .j a v a 2s. c o m public static VUUpdateInfo getUpdateInfo(String pluginID) throws IOException { final URLConnection conn = new URL("https://api.curseforge.com/servermods/files?projectIds=" + pluginID) .openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("User-Agent", "VU/1.0"); conn.setDoOutput(true); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final String response = reader.readLine(); final JSONArray array = (JSONArray) JSONValue.parse(response); final Matcher m = VUUpdater.vuPattern .matcher((String) ((JSONObject) array.get(array.size() - 1)).get("name")); if (!m.matches()) throw new IllegalArgumentException("No match found!"); final byte[] vuBytes = VUUpdater.hexStringToByteArray(m.group(4)); return new VUUpdateInfo(m.group(2), vuBytes); }
From source file:com.pa165.ddtroops.console.client.Application.java
/** * Get specific hero with unique id/*from w w w .j a va2s . co m*/ * * @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 ww w.j a v a2 s.c om*/ * @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/*w w w .ja va 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); } }