List of usage examples for java.net HttpURLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:com.cnaude.mutemanager.UUIDFetcher.java
private static HttpURLConnection createConnection() throws Exception { URL url = new URL(PROFILE_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false);//from w w w . java 2 s. co m connection.setDoInput(true); connection.setDoOutput(true); return connection; }
From source file:com.google.api.GoogleAPI.java
/** * Forms an HTTP request, sends it using POST method and returns the result of the request as a JSONObject. * /*from ww w . ja va 2s . com*/ * @param url The URL to query for a JSONObject. * @param parameters Additional POST parameters * @return The translated String. * @throws Exception on error. */ protected static JSONObject retrieveJSON(final URL url, final String parameters) throws Exception { try { final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("referer", referrer); uc.setRequestMethod("POST"); uc.setDoOutput(true); final PrintWriter pw = new PrintWriter(uc.getOutputStream()); pw.write(parameters); pw.flush(); try { final String result = inputStreamToString(uc.getInputStream()); return new JSONObject(result); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) { uc.getErrorStream().close(); } pw.close(); } } catch (Exception ex) { throw new Exception("[google-api-translate-java] Error retrieving translation.", ex); } }
From source file:Main.java
public static String customrequestget(String url, HashMap<String, String> map, String method) { if (null != map) { int i = 0; for (Map.Entry<String, String> entry : map.entrySet()) { if (i == 0) { url = url + "?" + entry.getKey() + "=" + entry.getValue(); } else { url = url + "&" + entry.getKey() + "=" + entry.getValue(); }// ww w .j a v a 2 s .c o m i++; } } try { URL murl = new URL(url); System.out.print(url); HttpURLConnection conn = (HttpURLConnection) murl.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod(method); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)"); InputStream inStream = conn.getInputStream(); String result = inputStream2String(inStream); conn.disconnect(); return result; } catch (Exception e) { // TODO: handle exception } return null; }
From source file:Main.java
static void setBasicAuthentication(HttpURLConnection conn, URL url) { String userInfo = url.getUserInfo(); if (userInfo != null && userInfo.length() > 0) { String authString = Base64.encodeToString(userInfo.getBytes(), Base64.DEFAULT); conn.setRequestProperty("Authorization", "Basic " + authString); }//from ww w.ja va2 s . c o m }
From source file:Main.java
public static String getRequest(String query) { HttpURLConnection connection = null; try {//from w w w . java 2s. c o m connection = (HttpURLConnection) new URL(url + query).openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept-Charset", charset); statusCode = connection.getResponseCode(); if (statusCode != 200) { return null; } InputStream response = connection.getInputStream(); BufferedReader bR = new BufferedReader(new InputStreamReader(response)); String line = ""; StringBuilder responseStrBuilder = new StringBuilder(); while ((line = bR.readLine()) != null) { responseStrBuilder.append(line); } response.close(); return responseStrBuilder.toString(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:com.intellectualcrafters.plot.uuid.UUIDFetcher.java
private static HttpURLConnection createConnection() throws Exception { final URL url = new URL(PROFILE_URL); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false);//w w w.ja v a2 s .co m connection.setDoInput(true); connection.setDoOutput(true); return connection; }
From source file:com.infosupport.service.LPRServiceCaller.java
public static JSONObject doGet(String urlString) { String json = null;// www .j a v a2 s. com try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; while ((output = br.readLine()) != null) { json = output; System.out.println(json + "from doGet"); } conn.disconnect(); } catch (IOException e) { Log.w(TAG, "IOException, waarschijnlijk geen internet connectie aanwezig..."); } JSONObject jsonObject = null; try { jsonObject = new JSONObject(json); } catch (JSONException e) { Log.e(TAG, "Kon geen JsonObject maken van het response"); } return jsonObject; }
From source file:com.monibox.selTestNG.utils.GetNodeIP.java
private static HttpURLConnection createHttpCon(final String url, final String method) throws IOException { final HttpURLConnection httpCon; httpCon = (HttpURLConnection) new URL(url).openConnection(); httpCon.setDoOutput(true);// www. j a v a2 s . c om httpCon.setRequestMethod(method); httpCon.setRequestProperty("Content-type", "application/json"); httpCon.setRequestProperty("accept", "application/json"); return httpCon; }
From source file:jmc.util.UtlFbComents.java
public static String getJSONComentarios(String url, Long numComents) throws JMCException { String linea = ""; String buf = ""; try {/*w w w .j a va2 s . c o m*/ Properties props = ConfigPropiedades.getProperties("props_config.properties"); CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); URL ur = new URL( "http://graph.facebook.com/comments?id=" + url + "&limit=" + numComents + "&filter=stream"); HttpURLConnection cn = (HttpURLConnection) ur.openConnection(); cn.setRequestProperty("user-agent", props.getProperty("navegador")); cn.setInstanceFollowRedirects(false); cn.setUseCaches(false); cn.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream())); while ((linea = br.readLine()) != null) { buf.concat(linea); } cn.disconnect(); } catch (IOException e) { throw new JMCException(e); } return buf; }
From source file:GoogleAPI.java
/** * Forms an HTTP request, sends it using GET method and returns the result of the request as a JSONObject. * //from ww w. java 2 s. co m * @param url The URL to query for a JSONObject. * @return The translated String. * @throws Exception on error. */ protected static JSONObject retrieveJSON(final URL url) throws Exception { try { final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("referer", referrer); uc.setRequestMethod("GET"); uc.setDoOutput(true); try { final String result = inputStreamToString(uc.getInputStream()); return new JSONObject(result); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) { uc.getErrorStream().close(); } } } catch (Exception ex) { throw new Exception("[google-api-translate-java] Error retrieving translation.", ex); } }