List of usage examples for java.net HttpURLConnection getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:edu.jhu.cvrg.timeseriesstore.opentsdb.AnnotationManager.java
public static String createIntervalAnnotation(String urlString, long startEpoch, long endEpoch, String tsuid, String description, String notes) { urlString = urlString + API_METHOD;// ww w.ja v a 2s. c om String result = ""; try { HttpURLConnection httpConnection = TimeSeriesUtility.openHTTPConnectionPOST(urlString); OutputStreamWriter wr = new OutputStreamWriter(httpConnection.getOutputStream()); JSONObject requestObject = new JSONObject(); requestObject.put("startTime", startEpoch); requestObject.put("endTime", endEpoch); requestObject.put("tsuid", tsuid); requestObject.put("description", description); requestObject.put("notes", notes); wr.write(requestObject.toString()); wr.close(); result = TimeSeriesUtility.readHttpResponse(httpConnection); } catch (IOException e) { e.printStackTrace(); return null; } catch (OpenTSDBException e) { e.printStackTrace(); result = String.valueOf(e.responseCode); } catch (JSONException e) { e.printStackTrace(); } return result; }
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);//from w w w . jav a 2 s. c o m DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(data); 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:com.telefonica.iot.perseo.test.Help.java
public static Res sendMethod(String url, String body, String method) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod(method);//from w ww . j a v a2 s . c o m con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(body); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); String responseBody = getBodyResponse(con); return new Res(responseCode, responseBody); }
From source file:edu.jhu.cvrg.timeseriesstore.opentsdb.TimeSeriesRetriever.java
private static JSONArray retrieveTimeSeriesPOST(String urlString, long startEpoch, long endEpoch, String metric, HashMap<String, String> tags) throws OpenTSDBException { urlString = urlString + API_METHOD;/* ww w . j av a 2 s . c om*/ String result = ""; try { HttpURLConnection httpConnection = TimeSeriesUtility.openHTTPConnectionPOST(urlString); OutputStreamWriter wr = new OutputStreamWriter(httpConnection.getOutputStream()); JSONObject mainObject = new JSONObject(); mainObject.put("start", startEpoch); mainObject.put("end", endEpoch); JSONArray queryArray = new JSONArray(); JSONObject queryParams = new JSONObject(); queryParams.put("aggregator", "sum"); queryParams.put("metric", metric); queryArray.put(queryParams); if (tags != null) { JSONObject queryTags = new JSONObject(); Iterator<Entry<String, String>> entries = tags.entrySet().iterator(); while (entries.hasNext()) { @SuppressWarnings("rawtypes") Map.Entry entry = (Map.Entry) entries.next(); queryTags.put((String) entry.getKey(), (String) entry.getValue()); } queryParams.put("tags", queryTags); } mainObject.put("queries", queryArray); String queryString = mainObject.toString(); wr.write(queryString); wr.flush(); wr.close(); result = TimeSeriesUtility.readHttpResponse(httpConnection); } catch (IOException e) { throw new OpenTSDBException("Unable to connect to server", e); } catch (JSONException e) { throw new OpenTSDBException("Error on request data", e); } return TimeSeriesUtility.makeResponseJSONArray(result); }
From source file:Main.java
private static String httpPost(String address, String params) { InputStream inputStream = null; HttpURLConnection urlConnection = null; try {//w ww .j a va 2 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:com.evrythng.java.wrapper.util.FileUtils.java
/** * Reads from {@link InputStream} provided and uploads data to Cloud as a file with public read access. * * @param url upload url./*from w ww .ja v a 2 s . com*/ * @param contentType content type. * @param stream {@link InputStream} where to read from. Should be closed externally. */ public static void uploadPublicStream(final URL url, final String contentType, final InputStream stream) throws IOException { HttpURLConnection connection = getConnectionForPublicUpload(url, contentType); try (OutputStream output = connection.getOutputStream()) { IOUtils.copy(stream, output); } validateConnectionAfterUpload(connection); }
From source file:Yak_Hax.Yak_Hax_Mimerme.PostRequest.java
public static String PostBodyRequest(String URL, String JSONRaw, String UserAgent) throws IOException { String type = "application/json"; URL u = new URL(URL); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setDoOutput(true);/*from w w w . j a v a2s .c o m*/ conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", type); conn.setRequestProperty("User-Agent", UserAgent); OutputStream os = conn.getOutputStream(); os.write(JSONRaw.getBytes()); os.flush(); os.close(); String response = null; DataInputStream input = new DataInputStream(conn.getInputStream()); while (null != ((response = input.readLine()))) { input.close(); return response; } return null; }
From source file:com.evrythng.java.wrapper.util.FileUtils.java
/** * Uploads text as a file content with public read access. * * @param url upload url./*from w w w . j av a 2 s . c o m*/ * @param contentType content type. * @param content text content. */ public static void uploadPublicContent(final URL url, final String contentType, final String content) throws IOException { HttpURLConnection connection = getConnectionForPublicUpload(url, contentType); try (OutputStream output = connection.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(output)) { bos.write(content.getBytes()); } validateConnectionAfterUpload(connection); }
From source file:com.evrythng.java.wrapper.util.FileUtils.java
/** * Uploads file with public read access. * * @param url upload url.// w w w . j ava 2 s .co m * @param contentType content type. * @param file file for upload. */ public static void uploadPublicFile(final URL url, final String contentType, final File file) throws IOException { HttpURLConnection connection = getConnectionForPublicUpload(url, contentType); try (OutputStream output = connection.getOutputStream(); WritableByteChannel wbc = Channels.newChannel(output); FileInputStream fis = new FileInputStream(file); FileChannel fc = fis.getChannel()) { fc.transferTo(0, fc.size(), wbc); } validateConnectionAfterUpload(connection); }
From source file:io.fares.junit.soapui.outside.test.SoapUIMockRunnerTest.java
public static String testMockService(String endpoint) throws MalformedURLException, IOException { HttpURLConnection con = (HttpURLConnection) new URL(endpoint).openConnection(); con.setRequestMethod("POST"); con.addRequestProperty("Accept", "application/soap+xml"); con.addRequestProperty("Content-Type", "application/soap+xml"); con.setDoOutput(true);//from ww w .java 2 s . c o m con.getOutputStream().write( "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header/><soap:Body><GetWeather xmlns=\"http://www.webserviceX.NET\"><CityName>Brisbane</CityName></GetWeather></soap:Body></soap:Envelope>" .getBytes("UTF-8")); InputStream is = con.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); String rs = writer.toString(); LOG.fine(rs); return rs; }