List of usage examples for java.net HttpURLConnection setDoOutput
public void setDoOutput(boolean dooutput)
From source file:Main.java
static public String downloadFile(String downloadUrl, String fileName, String dir) throws IOException { URL url = new URL(downloadUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.connect();/* w ww .jav a 2 s . c om*/ int code = urlConnection.getResponseCode(); if (code > 300 || code == -1) { throw new IOException("Cannot read url: " + downloadUrl); } String filePath = prepareFilePath(fileName, dir); String tmpFilePath = prepareTmpFilePath(fileName, dir); FileOutputStream fileOutput = new FileOutputStream(tmpFilePath); BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream()); byte[] buffer = new byte[1024]; int bufferLength; while ((bufferLength = inputStream.read(buffer)) > 0) { fileOutput.write(buffer, 0, bufferLength); } fileOutput.close(); // move tmp to destination copyFile(new File(tmpFilePath), new File(filePath)); return filePath; }
From source file:Main.java
public static HttpURLConnection buildConnection(String url, String requestMethod) throws IOException { HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); /*set request*/ con.setRequestMethod(requestMethod); con.setRequestProperty("Accept-Language", "UTF-8"); con.setDoOutput(true); return con;/*w w w . ja va 2s.c o m*/ }
From source file:ca.xecure.easychip.CommonUtilities.java
public static void http_post(String endpoint, Map<String, String> params) throws IOException { URL url;/* w ww . ja v a 2 s . com*/ try { url = new URL(endpoint); } catch (MalformedURLException e) { throw new IllegalArgumentException("invalid url: " + endpoint); } String body = JSONValue.toJSONString(params); Log.v(LOG_TAG, "Posting '" + body + "' to " + url); byte[] bytes = body.getBytes(); HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); int status = conn.getResponseCode(); if (status != 200) { throw new IOException("Post failed with error code " + status); } } finally { if (conn != null) { conn.disconnect(); } } }
From source file:lapispaste.Main.java
private static void paster(Map<String, String> pastemap, StringBuffer pdata) { try {// w w w . j av a 2 s. com URL url = new URL("http://paste.linux-sevenler.org/index.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // set connection 'writeable' conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); // construct the data... String data; String pdataString = pdata.toString(); data = URLEncoder.encode("language", "ISO-8859-9") + "=" + URLEncoder.encode(pastemap.get("format"), "ISO-8859-9"); data += "&" + URLEncoder.encode("source", "ISO-8859-9") + "=" + URLEncoder.encode(pdataString, "ISO-8859-9"); data += "&" + URLEncoder.encode("submit", "ISO-8859-9") + "=" + URLEncoder.encode(" Kaydet ", "ISO-8859-9"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // get new url where the paste is conn.getInputStream(); System.out.println(conn.getURL()); conn.disconnect(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:connector.ISConnector.java
public static JSONObject processRequest(String servlet, byte[] query) { JSONObject response = null;/*ww w . j av a2 s. c om*/ if (servlet != null && !servlet.startsWith("/")) servlet = "/" + servlet; try { // Establish HTTP connection with Identity Service URL url = new URL(CONTEXT_PATH + servlet); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setAllowUserInteraction(false); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //Create the form content try (OutputStream out = conn.getOutputStream()) { out.write(query); out.close(); } // Buffer the result into a string BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); response = (JSONObject) new JSONParser().parse(sb.toString()); } catch (Exception e) { e.printStackTrace(); } return response; }
From source file:com.mopaas_mobile.http.BaseHttpRequester.java
public static String doPOST(String urlstr, List<BasicNameValuePair> params) throws IOException { String result = null;//from ww w.j a v a2 s. c om URL url = new URL(urlstr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(false); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // connection.setRequestProperty("token",token); connection.setConnectTimeout(30000); connection.setReadTimeout(30000); connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); String content = ""; if (params != null && params.size() > 0) { for (int i = 0; i < params.size(); i++) { content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "=" + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8"); } out.writeBytes(content.substring(1)); } out.flush(); out.close(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer b = new StringBuffer(); int ch; while ((ch = br.read()) != -1) { b.append((char) ch); } result = b.toString().trim(); connection.disconnect(); return result; }
From source file:Main.java
public static String executePost(String targetURL, String urlParameters) { try {/*from w w w.j av a 2 s . c om*/ HttpURLConnection connection = (HttpURLConnection) new URL(targetURL).openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); //connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); OutputStream output = null; try { output = connection.getOutputStream(); output.write(urlParameters.getBytes()); } finally { if (output != null) try { output.flush(); output.close(); } catch (IOException logOrIgnore) { } } InputStream response = connection.getInputStream(); String contentType = connection.getHeaderField("Content-Type"); String responseStr = ""; if (true) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(response)); for (String line; (line = reader.readLine()) != null;) { //System.out.println(line); responseStr = responseStr + line; Thread.sleep(2); } } finally { if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) { } } } else { // It's likely binary content, use InputStream/OutputStream. System.out.println("Binary content"); } return responseStr; } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:editor.util.URLUtil.java
public static String sendPost(String url, String data) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(data);//from w w w. j a va 2 s .c o m 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(); return response.toString(); }
From source file:Main.java
private static String httpPost(String address, String params) { InputStream inputStream = null; HttpURLConnection urlConnection = null; try {/*from w w w .ja v a2 s. c o m*/ URL url = new URL(address); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoOutput(true); urlConnection.setRequestMethod("POST"); urlConnection.getOutputStream().write(params.getBytes()); urlConnection.getOutputStream().flush(); urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { inputStream = urlConnection.getInputStream(); int len = 0; byte[] buffer = new byte[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((len = inputStream.read(buffer)) != -1) { stringBuffer.append(new String(buffer, 0, len)); } return stringBuffer.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(inputStream); if (urlConnection != null) { urlConnection.disconnect(); } } return null; }
From source file:Main.java
/** * Issue a POST request to the server./* ww w. j a v a 2 s. c o m*/ * * @param endpoint * POST address. * @param params * request parameters. * * @throws java.io.IOException * propagated from POST. */ public static String post(String endpoint, Map<String, String> params) throws IOException { URL url; try { url = new URL(endpoint); } catch (MalformedURLException e) { throw new IllegalArgumentException("invalid url: " + endpoint); } StringBuilder bodyBuilder = new StringBuilder(); Iterator<Entry<String, String>> iterator = params.entrySet().iterator(); // constructs the POST body using the parameters while (iterator.hasNext()) { Entry<String, String> param = iterator.next(); bodyBuilder.append(param.getKey()).append('=').append(param.getValue()); if (iterator.hasNext()) { bodyBuilder.append('&'); } } String body = bodyBuilder.toString(); byte[] bytes = body.getBytes(); HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); // post the request OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); // handle the response int status = conn.getResponseCode(); if (status != 200) { throw new IOException("Post failed with error code " + status); } // Get Response InputStream is = conn.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\n'); } rd.close(); return response.toString(); } finally { if (conn != null) { conn.disconnect(); } } }