List of usage examples for java.io DataOutputStream close
@Override public void close() throws IOException
From source file:com.example.scandevice.MainActivity.java
public static String excutePost(String targetURL, String urlParameters) { URL url;/* w w w . ja v a 2s .c om*/ 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:Main.IrcBot.java
public static void postGit(String pasteBinSnippet, PrintWriter out) { try {/*from w w w . ja v a 2 s. c o m*/ 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) { } }
From source file:Main.java
private static boolean installOrUninstallApk(String apkPath, String installOruninstall, String rOrP) { Process process = null;// w w w. j a va2s . c om DataOutputStream os = null; String command = null; try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); command = "pm " + installOruninstall + " " + rOrP + " " + apkPath + " \n"; os.writeBytes(command); os.flush(); os.close(); process.waitFor(); process.destroy(); return true; } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return false; }
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 . j av a2s . co m confOutput.flush(); confOutput.close(); }
From source file:com.reactivetechnologies.jaxrs.RestServerTest.java
static String sendPost(String url, String content) throws IOException { StringBuilder response = new StringBuilder(); HttpURLConnection con = null; BufferedReader in = null;//from w w w . ja v a 2s . co m try { URL obj = new URL(url); con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("POST"); //add request header con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeUTF(content); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } } else { throw new IOException("Response Code: " + responseCode); } return response.toString(); } catch (IOException e) { throw e; } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (con != null) { con.disconnect(); } } }
From source file:com.reactivetechnologies.jaxrs.RestServerTest.java
static String sendDelete(String url, String content) throws IOException { StringBuilder response = new StringBuilder(); HttpURLConnection con = null; BufferedReader in = null;//ww w .ja v a 2s .co m try { URL obj = new URL(url); con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("DELETE"); //add request header con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeUTF(content); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } } else { throw new IOException("Response Code: " + responseCode); } return response.toString(); } catch (IOException e) { throw e; } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (con != null) { con.disconnect(); } } }
From source file:com.autonavi.gxdtaojin.toolbox.HurlStack.java
@SuppressWarnings("deprecation") /* package */static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: // This is the deprecated way that needs to be handled for backwards compatibility. // If the request's post body is null, then the assumption is that the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { // Prepare output. There is no need to set Content-Length explicitly, // since this is handled by HttpURLConnection using the size of the prepared // output stream. connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(postBody);// w ww .j av a 2 s.co m out.close(); } break; case Method.GET: // Not necessary to set the request method because connection defaults to GET but // being explicit here. connection.setRequestMethod("GET"); break; case Method.DELETE: connection.setRequestMethod("DELETE"); break; case Method.POST: connection.setRequestMethod("POST"); addBodyIfExists(connection, request); break; case Method.PUT: connection.setRequestMethod("PUT"); addBodyIfExists(connection, request); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java
private static void addBodyIfExists(HttpURLConnection connection, final Request<?> request) throws IOException, AuthFailureError { connection.setDoOutput(true);/*from w w w . j av a 2 s .co m*/ connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType()); if (request instanceof MultiPartRequest) { final UploadMultipartEntity multipartEntity = ((MultiPartRequest<?>) request).getMultipartEntity(); // connection.setFixedLengthStreamingMode((int) multipartEntity.getContentLength()); multipartEntity.writeTo(connection.getOutputStream()); } else { byte[] body = request.getBody(); if (body != null) { DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(body); out.close(); } } }
From source file:com.amazon.pay.impl.Util.java
/** * This method uses HttpURLConnection instance to make requests. * * @param method The HTTP method (GET,POST,PUT,etc.). * @param url The URL/*from ww w. j a va 2s .c om*/ * @param urlParameters URL Parameters * @param headers Header key-value pairs * @return ResponseData * @throws IOException */ public static ResponseData httpSendRequest(String method, String url, String urlParameters, Map<String, String> headers) throws IOException { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); if (headers != null && !headers.isEmpty()) { for (String key : headers.keySet()) { con.setRequestProperty(key, headers.get(key)); } } con.setDoOutput(true); con.setRequestMethod(method); if (urlParameters != null) { DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); } int responseCode = con.getResponseCode(); BufferedReader in; if (responseCode != 200) { in = new BufferedReader(new InputStreamReader(con.getErrorStream())); } else { in = new BufferedReader(new InputStreamReader(con.getInputStream())); } String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine).append(LINE_SEPARATOR); } in.close(); return new ResponseData(responseCode, response.toString()); }
From source file:com.aiven.seafox.controller.http.volley.toolbox.HurlStack.java
@SuppressWarnings("deprecation") /* package */ static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: // This is the deprecated way that needs to be handled for backwards compatibility. // If the request's post body is null, then the assumption is that the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { // Prepare output. There is no need to set Content-Length explicitly, // since this is handled by HttpURLConnection using the size of the prepared // output stream. connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(postBody);//from www . j a v a 2s.c om out.close(); } break; case Method.GET: // Not necessary to set the request method because connection defaults to GET but // being explicit here. connection.setRequestMethod("GET"); break; case Request.Method.DELETE: connection.setRequestMethod("DELETE"); break; case Request.Method.POST: connection.setRequestMethod("POST"); addBodyIfExists(connection, request); break; case Method.PUT: connection.setRequestMethod("PUT"); addBodyIfExists(connection, request); break; case Method.HEAD: connection.setRequestMethod("HEAD"); break; case Method.OPTIONS: connection.setRequestMethod("OPTIONS"); break; case Method.TRACE: connection.setRequestMethod("TRACE"); break; case Method.PATCH: connection.setRequestMethod("PATCH"); addBodyIfExists(connection, request); break; default: throw new IllegalStateException("Unknown method type."); } }