List of usage examples for java.net HttpURLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:yodlee.ysl.api.io.HTTP.java
public static String doPost(String url, String requestBody) throws IOException { String mn = "doIO(POST : " + url + ", " + requestBody + " )"; System.out.println(fqcn + " :: " + mn); URL restURL = new URL(url); HttpURLConnection conn = (HttpURLConnection) restURL.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", userAgent); //conn.setRequestProperty("Content-Type", contentTypeURLENCODED); conn.setRequestProperty("Content-Type", contentTypeJSON); conn.setDoOutput(true);/* w w w. j ava2 s.com*/ DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(requestBody); wr.flush(); wr.close(); int responseCode = conn.getResponseCode(); System.out.println(fqcn + " :: " + mn + " : " + "Sending 'HTTP POST' request"); System.out.println(fqcn + " :: " + mn + " : " + "Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuilder jsonResponse = new StringBuilder(); while ((inputLine = in.readLine()) != null) { jsonResponse.append(inputLine); } in.close(); System.out.println(fqcn + " :: " + mn + " : " + jsonResponse.toString()); return new String(jsonResponse); }
From source file:com.brobwind.brodm.NetworkUtils.java
public static void deviceInfo(String path, OnMessage callback) { try {/*from w w w . j a va 2s . c o m*/ URL url = new URL(path + "/privet/info"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic anonymous"); urlConnection.setRequestProperty("Content-Type", "application/json"); urlConnection.connect(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); BufferedReader bufReader = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); for (String line = bufReader.readLine(); line != null;) { sb.append(line).append("\n"); line = bufReader.readLine(); } callback.onMessage(sb.toString(), null); return; } finally { urlConnection.disconnect(); } } catch (MalformedURLException muex) { Log.e(TAG, "Malformed url: " + path); callback.onMessage(null, muex.getMessage()); } catch (Exception e) { e.printStackTrace(); callback.onMessage(null, e.getMessage()); } }
From source file:yodlee.ysl.api.io.HTTP.java
public static String doPutNew(String url, String param, Map<String, String> sessionTokens) throws IOException, URISyntaxException { String mn = "doIO(PUT :" + url + ", sessionTokens = " + sessionTokens.toString() + " )"; System.out.println(fqcn + " :: " + mn); //param=param.replace("\"", "%22").replace("{", "%7B").replace("}", "%7D").replace(",", "%2C").replace("[", "%5B").replace("]", "%5D").replace(":", "%3A").replace(" ", "+"); String processedURL = url;//+"?MFAChallenge="+param;//"%7B%22loginForm%22%3A%7B%22formType%22%3A%22token%22%2C%22mfaTimeout%22%3A%2299380%22%2C%22row%22%3A%5B%7B%22id%22%3A%22token_row%22%2C%22label%22%3A%22Security+Key%22%2C%22form%22%3A%220001%22%2C%22fieldRowChoice%22%3A%220001%22%2C%22field%22%3A%5B%7B%22id%22%3A%22token%22%2C%22name%22%3A%22tokenValue%22%2C%22type%22%3A%22text%22%2C%22value%22%3A%22123456%22%2C%22isOptional%22%3Afalse%2C%22valueEditable%22%3Atrue%2C%22maxLength%22%3A%2210%22%7D%5D%7D%5D%7D%7D"; URL myURL = new URL(processedURL); System.out.println(fqcn + " :: " + mn + ": Request URL=" + processedURL.toString()); HttpURLConnection conn = (HttpURLConnection) myURL.openConnection(); conn.setRequestMethod("PUT"); conn.setRequestProperty("Accept-Charset", "UTF-8"); conn.setRequestProperty("Content-Type", contentTypeJSON); conn.setRequestProperty("Authorization", sessionTokens.toString()); conn.setDoOutput(true);//from w ww .j av a 2 s . c om DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(param); wr.flush(); wr.close(); System.out.println(fqcn + " :: " + mn + " : " + "Sending 'HTTP PUT' request"); int responseCode = conn.getResponseCode(); System.out.println(fqcn + " :: " + mn + " : " + "Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuilder jsonResponse = new StringBuilder(); while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); jsonResponse.append(inputLine); } in.close(); System.out.println(fqcn + " :: " + mn + " : " + jsonResponse.toString()); return new String(jsonResponse); }
From source file:net.andylizi.colormotd.anim.utils.AttributionUtil.java
private static String sendGet(String url, String param, String charset) { StringBuilder result = new StringBuilder(1024); BufferedReader in = null;// w ww .j a v a2 s . com try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); conn.setRequestProperty("User-Agent", Main.getInstance().getDescription().getFullName()); conn.setRequestProperty("Accept-Charset", charset); conn.setUseCaches(true); conn.setConnectTimeout(2000); conn.setReadTimeout(3000); conn.connect(); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), Charset.forName(charset)), 1024); String line; while ((line = in.readLine()) != null) { result.append(line); } conn.disconnect(); } catch (Exception e) { } finally { try { if (in != null) { in.close(); } } catch (Exception e2) { } } return result.toString(); }
From source file:org.openo.nfvo.monitor.dac.util.APIHttpClient.java
public static void doDelete(String urls, String token) { URL url = null;// ww w . j av a 2 s .c o m try { url = new URL(urls); } catch (MalformedURLException exception) { exception.printStackTrace(); } HttpURLConnection httpURLConnection = null; try { httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestProperty("Content-Type", "application/json"); if (!Global.isEmpty(token)) { httpURLConnection.setRequestProperty("X-Auth-Token", token); } httpURLConnection.setRequestMethod("DELETE"); logger.info("#####====" + httpURLConnection.getResponseCode()); } catch (IOException e) { logger.error("Exception", e); } finally { if (httpURLConnection != null) { httpURLConnection.disconnect(); } } }
From source file:yodlee.ysl.api.io.HTTP.java
public static String doPostUser(String url, Map<String, String> sessionTokens, String requestBody, boolean isEncodingNeeded) throws IOException { String mn = "doIO(POST : " + url + ", " + requestBody + "sessionTokens : " + sessionTokens + " )"; System.out.println(fqcn + " :: " + mn); URL restURL = new URL(url); HttpURLConnection conn = (HttpURLConnection) restURL.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", userAgent); if (isEncodingNeeded) //conn.setRequestProperty("Content-Type", contentTypeURLENCODED); conn.setRequestProperty("Content-Type", contentTypeJSON); else/*from w w w . j ava 2 s .c o m*/ conn.setRequestProperty("Content-Type", "text/plain;charset=UTF-8"); conn.setRequestProperty("Authorization", sessionTokens.toString()); conn.setDoOutput(true); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(requestBody); wr.flush(); wr.close(); int responseCode = conn.getResponseCode(); System.out.println(fqcn + " :: " + mn + " : " + "Sending 'HTTP POST' request"); System.out.println(fqcn + " :: " + mn + " : " + "Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuilder jsonResponse = new StringBuilder(); while ((inputLine = in.readLine()) != null) { jsonResponse.append(inputLine); } in.close(); System.out.println(fqcn + " :: " + mn + " : " + jsonResponse.toString()); return new String(jsonResponse); }
From source file:flow.visibility.tapping.OpenDaylightHelper.java
/** The function for inserting the flow */ public static boolean installFlow(JSONObject postData, String user, String password, String baseURL) { StringBuffer result = new StringBuffer(); /** Check the connection to ODP REST API page */ try {//from ww w .ja v a 2s .c o m if (!baseURL.contains("http")) { baseURL = "http://" + baseURL; } baseURL = baseURL + "/controller/nb/v2/flowprogrammer/default/node/OF/" + postData.getJSONObject("node").get("id") + "/staticFlow/" + postData.get("name"); /** Create URL = base URL + container */ URL url = new URL(baseURL); /** Create authentication string and encode it to Base64*/ String authStr = user + ":" + password; String encodedAuthStr = Base64.encodeBase64String(authStr.getBytes()); /** Create Http connection */ HttpURLConnection connection = (HttpURLConnection) url.openConnection(); /** Set connection properties */ connection.setRequestMethod("PUT"); connection.setRequestProperty("Authorization", "Basic " + encodedAuthStr); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); /** Set JSON Post Data */ OutputStream os = connection.getOutputStream(); os.write(postData.toString().getBytes()); os.close(); /** Get the response from connection's inputStream */ InputStream content = (InputStream) connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(content)); String line = ""; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); } /** checking the result of REST API connection */ if ("success".equalsIgnoreCase(result.toString())) { return true; } else { return false; } }
From source file:software.uncharted.util.HTTPUtil.java
public static String post(String urlToRead, String postData) { URL url;/*from w w w .j av a 2s. c o m*/ HttpURLConnection conn; try { url = new URL(urlToRead); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/json"); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(postData); wr.flush(); wr.close(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[16384]; InputStream is = conn.getInputStream(); while ((nRead = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); return buffer.toString(); } catch (Exception e) { e.printStackTrace(); System.err.println("Failed to read URL: " + urlToRead + " <" + e.getMessage() + ">"); } return null; }
From source file:com.zf.util.Post_NetNew.java
public static String pn(Map<String, String> pams) throws Exception { if (null == pams) { return ""; }//from w ww . ja v a2s.c o m String strtmp = "url"; URL url = new URL(pams.get(strtmp)); pams.remove(strtmp); strtmp = "body"; String body = pams.get(strtmp); pams.remove(strtmp); strtmp = "POST"; if (StringUtils.isEmpty(body)) strtmp = "GET"; HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); httpConn.setUseCaches(false); httpConn.setRequestMethod(strtmp); for (String pam : pams.keySet()) { httpConn.setRequestProperty(pam, pams.get(pam)); } if ("POST".equals(strtmp)) { httpConn.setDoOutput(true); httpConn.setDoInput(true); DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream()); dos.writeBytes(body); dos.flush(); } int resultCode = httpConn.getResponseCode(); StringBuilder sb = new StringBuilder(); sb.append(resultCode).append("\n"); String readLine; InputStream stream; try { stream = httpConn.getInputStream(); } catch (Exception ignored) { stream = httpConn.getErrorStream(); } try { BufferedReader responseReader = new BufferedReader(new InputStreamReader(stream, "UTF-8")); while ((readLine = responseReader.readLine()) != null) { sb.append(readLine).append("\n"); } } catch (Exception ignored) { } return sb.toString(); }
From source file:Main.java
public static String getHttpText(String urlInfo, String userName, String password) throws MalformedURLException, IOException { URL url = null;//w ww.ja v a 2 s . c o m String tempStr = null; url = new URL(urlInfo); HttpURLConnection huc = null; String credit = userName + ":" + password; String encoding = new sun.misc.BASE64Encoder().encode(credit.getBytes()); StringBuffer sb = new StringBuffer(); huc = (HttpURLConnection) url.openConnection(); huc.setAllowUserInteraction(false); huc.setRequestProperty("Authorization", "Basic " + encoding); BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream(), "utf-8")); String line = null; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } tempStr = sb.toString(); return tempStr; }