List of usage examples for java.net HttpURLConnection setDoOutput
public void setDoOutput(boolean dooutput)
From source file:es.tid.cep.esperanza.Utils.java
public static boolean DoHTTPPost(String urlStr, String content) { try {/*from ww w . j a v a2 s .co m*/ URL url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); OutputStreamWriter printout = new OutputStreamWriter(urlConn.getOutputStream(), Charset.forName("UTF-8")); printout.write(content); printout.flush(); printout.close(); int code = urlConn.getResponseCode(); String message = urlConn.getResponseMessage(); logger.debug("action http response " + code + " " + message); if (code / 100 == 2) { InputStream input = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); for (String line; (line = reader.readLine()) != null;) { logger.debug("action response body: " + line); } input.close(); return true; } else { logger.error("action response is not OK: " + code + " " + message); InputStream error = urlConn.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(error)); for (String line; (line = reader.readLine()) != null;) { logger.error("action error response body: " + line); } error.close(); return false; } } catch (MalformedURLException me) { logger.error("exception MalformedURLException: " + me.getMessage()); return false; } catch (IOException ioe) { logger.error("exception IOException: " + ioe.getMessage()); return false; } }
From source file:cc.vileda.sipgatesync.api.SipgateApi.java
public static String getToken(final String username, final String password) { try {//from www . j a va2 s .com final HttpURLConnection urlConnection = getConnection("/authorization/token"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setRequestMethod("POST"); JSONObject request = new JSONObject(); request.put("username", username); request.put("password", password); OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream()); Log.d("SipgateApi", request.getString("username")); wr.write(request.toString()); wr.flush(); StringBuilder sb = new StringBuilder(); int HttpResult = urlConnection.getResponseCode(); if (HttpResult == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), "utf-8")); String line; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } br.close(); Log.d("SipgateApi", "" + sb.toString()); final JSONObject response = new JSONObject(sb.toString()); return response.getString("token"); } else { System.out.println(urlConnection.getResponseMessage()); } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String upLoad(File file, String RequestURL) { String BOUNDER = UUID.randomUUID().toString(); String PREFIX = "--"; String END = "/r/n"; try {/* w ww. j a va 2 s . c om*/ URL url = new URL(RequestURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(TIME_OUT); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Charset", CHARSET); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cotent-Type", CONTENT_TYPE + ";boundary=" + BOUNDER); if (file != null) { OutputStream outputStream = connection.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); StringBuffer sb = new StringBuffer(); sb.append(PREFIX); sb.append(BOUNDER + END); dataOutputStream.write(sb.toString().getBytes()); InputStream in = new FileInputStream(file); byte[] b = new byte[1024]; int l = 0; while ((l = in.read()) != -1) { outputStream.write(b, 0, l); } in.close(); dataOutputStream.write(END.getBytes()); dataOutputStream.write((PREFIX + BOUNDER + PREFIX + END).getBytes()); dataOutputStream.flush(); int i = connection.getResponseCode(); if (i == 200) { return SUCCESS; } } } catch (IOException e) { e.printStackTrace(); } return FALIURE; }
From source file:Main.java
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties) throws IOException { HttpURLConnection urlConnection = null; try {/* w ww. j ava2 s. co m*/ urlConnection = (HttpURLConnection) new URL(url).openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(data != null); urlConnection.setDoInput(true); if (requestProperties != null) { for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) { urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue()); } } if (data != null) { OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); out.write(data); out.close(); } InputStream in = new BufferedInputStream(urlConnection.getInputStream()); return convertInputStreamToByteArray(in); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
From source file:com.github.terma.m.node.Node.java
private static void send(final String serverHost, final int serverPort, final String context, final List<Event> events) throws IOException { final HttpURLConnection connection = (HttpURLConnection) new URL("http", serverHost, serverPort, context + "/node").openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/json"); connection.setRequestProperty("charset", "utf-8"); connection.setUseCaches(false);/*from w w w. j a v a 2 s . c om*/ connection.setInstanceFollowRedirects(false); connection.connect(); OutputStream outputStream = connection.getOutputStream(); outputStream.write(new Gson().toJson(events).getBytes()); connection.getInputStream().read(); outputStream.close(); }
From source file:Main.java
/** * Issue a POST request to the server.//from w ww. j a v a 2 s . c o m * * @param endpoint POST address. * @param params request parameters. * @return response * @throws IOException propagated from POST. */ private static String executePost(String endpoint, Map<String, String> params) throws IOException { URL url; StringBuffer response = new StringBuffer(); 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(); //Log.v(TAG, "Posting '" + body + "' to " + url); byte[] bytes = body.getBytes(); HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(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); } else { BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); } } finally { if (conn != null) { conn.disconnect(); } } return response.toString(); }
From source file:com.eTilbudsavis.etasdk.network.impl.HttpURLNetwork.java
private static void addBodyIfExists(HttpURLConnection connection, Request<?> request) throws IOException { byte[] body = request.getBody(); if (body != null) { connection.setDoOutput(true); connection.addRequestProperty(HeaderUtils.CONTENT_TYPE, request.getBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(body);/*from ww w . jav a 2 s . co m*/ out.close(); } }
From source file:Main.java
/** * Do an HTTP POST and return the data as a byte array. *//*from w w w. j ava 2 s . c om*/ public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties) throws MalformedURLException, IOException { HttpURLConnection urlConnection = null; try { urlConnection = (HttpURLConnection) new URL(url).openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(data != null); urlConnection.setDoInput(true); if (requestProperties != null) { for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) { urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue()); } } if (data != null) { OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); out.write(data); out.close(); } InputStream in = new BufferedInputStream(urlConnection.getInputStream()); return convertInputStreamToByteArray(in); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
From source file:Main.java
private static HttpURLConnection initHttpURLConn(String requestURL) throws MalformedURLException, IOException, ProtocolException { URL url = new URL(requestURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(TIME_OUT); connection.setReadTimeout(TIME_OUT); connection.setDoInput(true);//from w w w .j av a2s . c o m connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Charset", CHARSET); connection.setRequestProperty("connection", "keep-alive"); return connection; }
From source file:Main.java
private static void post(String endpoint, Map<String, String> params) throws IOException { URL url;// w w w.j a v a 2 s. co m try { url = new URL(endpoint); } catch (MalformedURLException e) { throw new IllegalArgumentException("invalid url: " + endpoint); } StringBuilder bodyBuilder = new StringBuilder(); Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator(); // constructs the POST body using the parameters while (iterator.hasNext()) { Map.Entry<String, String> param = iterator.next(); bodyBuilder.append(param.getKey()).append('=').append(param.getValue()); if (iterator.hasNext()) { bodyBuilder.append('&'); } } String body = bodyBuilder.toString(); Log.v(TAG, "Posting '" + body + "' to " + url); byte[] bytes = body.getBytes(); HttpURLConnection conn = null; try { Log.e("URL", "> " + url); 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); } } finally { if (conn != null) { conn.disconnect(); } } }