List of usage examples for java.io DataOutputStream flush
public void flush() throws IOException
From source file:pushandroid.POST2GCM.java
public static void post(Content content) { try {//from ww w . j a v a2s .c o m // 1. URL URL url = new URL(URL); // 2. Open connection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 3. Specify POST method conn.setRequestMethod("POST"); // 4. Set the headers conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "key=" + apiKey); conn.setDoOutput(true); // 5. Add JSON data into POST request body //`5.1 Use Jackson object mapper to convert Contnet object into JSON ObjectMapper mapper = new ObjectMapper(); // 5.2 Get connection output stream DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); // 5.3 Copy Content "JSON" into mapper.writeValue(wr, content); // 5.4 Send the request wr.flush(); // 5.5 close wr.close(); // 6. Get the response int responseCode = conn.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 7. Print result System.out.println(response.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
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/* w ww . j a v a2 s .co 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:RemoteDeviceDiscovery.java
public static void postDevice(RemoteDevice d) throws Exception { String url = "http://bluetoothdatabase.com/datacollection/"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", "blucat"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = deviceJson(d); // Send post request con.setDoOutput(true);//ww w .j av a 2 s. c o m DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); }
From source file:Main.java
public static boolean getRootPermission(Context context) { String packageCodePath = context.getPackageCodePath(); Process process = null;// ww w . ja v a2 s. c o m DataOutputStream os = null; try { String cmd = "chmod 777 " + packageCodePath; process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(cmd + "\n"); os.writeBytes("exit\n"); os.flush(); process.waitFor(); } catch (Exception e) { return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { e.printStackTrace(); } } return true; }
From source file:net.maxgigapop.mrs.driver.openstack.OpenStackRESTClient.java
private static String sendPOST(URL url, HttpURLConnection con, String body) throws IOException { con.setRequestMethod("POST"); con.setRequestProperty("Content-type", "application/json"); con.setRequestProperty("Accept", "application/json"); con.setDoOutput(true);//from w w w .jav a 2 s.c om DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(body); wr.flush(); wr.close(); logger.log(Level.INFO, "Sending POST request to URL : {0}", url); int responseCode = con.getResponseCode(); logger.log(Level.INFO, "Response Code : {0}", responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder responseStr = new StringBuilder(); while ((inputLine = in.readLine()) != null) { responseStr.append(inputLine); } in.close(); return responseStr.toString(); }
From source file:Downloader.java
/** * Creates an URL connection for the specified URL and data. * //from ww w . ja v a 2 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:Main.IrcBot.java
public static void postRequest(String pasteCode, PrintWriter out) { try {/*from ww w.j a va 2 s . c om*/ String url = "http://pastebin.com/raw.php?i=" + pasteCode; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = ""; // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine + "\n"); } in.close(); //print result System.out.println(response.toString()); if (response.length() > 0) { IrcBot.postGit(response.toString(), out); } else { out.println("PRIVMSG #learnprogramming :The PasteBin URL is not valid."); } } catch (Exception p) { } }
From source file:com.example.scandevice.MainActivity.java
public static String excutePost(String targetURL, String urlParameters) { URL url;//from w ww . jav a 2 s. c o m HttpURLConnection connection = null; try { //Create connection url = new URL(targetURL); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); connection.setRequestProperty("Content-Language", "en-US"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); //Send request DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); } catch (Exception e) { e.printStackTrace(); return "Don't post data"; } finally { if (connection != null) { connection.disconnect(); } } return "Data Sent"; }
From source file:edu.uci.ics.pregelix.example.asterixdb.ConnectorTest.java
@SuppressWarnings("deprecation") private static void startHDFS() throws IOException { conf.addResource(new Path(PATH_TO_HADOOP_CONF + "/core-site.xml")); conf.addResource(new Path(PATH_TO_HADOOP_CONF + "/mapred-site.xml")); conf.addResource(new Path(PATH_TO_HADOOP_CONF + "/hdfs-site.xml")); FileSystem lfs = FileSystem.getLocal(new Configuration()); lfs.delete(new Path("build"), true); System.setProperty("hadoop.log.dir", "logs"); dfsCluster = new MiniDFSCluster(conf, numberOfNC, true, null); DataOutputStream confOutput = new DataOutputStream(new FileOutputStream(new File(HADOOP_CONF_PATH))); conf.writeXml(confOutput);//from www. ja va 2s .c o m confOutput.flush(); confOutput.close(); }
From source file:Main.IrcBot.java
public static void postGit(String pasteBinSnippet, PrintWriter out) { try {/*from w w w . java2s . com*/ String url = "https://api.github.com/gists"; URL obj = new URL(url); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); //add reuqest header JSONObject x = new JSONObject(); JSONObject y = new JSONObject(); JSONObject z = new JSONObject(); z.put("content", pasteBinSnippet); y.put("index.txt", z); x.put("public", true); x.put("description", "LPBOT"); x.put("files", y); con.setRequestMethod("POST"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = "public=true&description=LPBOT"; // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(x.toString()); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //print result JSONObject gitResponse = new JSONObject(response.toString()); String newGitUrl = gitResponse.getString("html_url"); if (newGitUrl.length() > 0) { out.println("PRIVMSG #learnprogramming :The paste on Git: " + newGitUrl); } System.out.println(newGitUrl); } catch (Exception p) { } }