List of usage examples for java.net HttpURLConnection getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:com.google.appengine.tck.logservice.RequestLogsTest.java
private String performPostRequest(URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); try {/*www . j av a2 s. c om*/ connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); PrintWriter out = new PrintWriter(connection.getOutputStream()); try { out.println("foo=bar"); } finally { out.close(); } return readFullyAndClose(connection.getInputStream()).trim(); } finally { connection.disconnect(); } }
From source file:net.bashtech.geobot.BotManager.java
public static String postRemoteDataStrawpoll(String urlString) { String line = ""; try {/*from w ww. j a v a 2s . c om*/ HttpURLConnection c = (HttpURLConnection) (new URL("http://strawpoll.me/api/v2/polls") .openConnection()); c.setRequestMethod("POST"); c.setRequestProperty("Content-Type", "application/json"); c.setRequestProperty("User-Agent", "CB2"); c.setDoOutput(true); c.setDoInput(true); c.setUseCaches(false); String queryString = urlString; c.setRequestProperty("Content-Length", Integer.toString(queryString.length())); DataOutputStream wr = new DataOutputStream(c.getOutputStream()); wr.writeBytes(queryString); wr.flush(); wr.close(); Scanner inStream = new Scanner(c.getInputStream()); while (inStream.hasNextLine()) line += (inStream.nextLine()); inStream.close(); System.out.println(line); try { JSONParser parser = new JSONParser(); Object obj = parser.parse(line); JSONObject jsonObject = (JSONObject) obj; line = (Long) jsonObject.get("id") + ""; } catch (Exception e) { e.printStackTrace(); } } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return line; }
From source file:gr.uoc.nlp.opinion.analysis.suggestion.ElasticSearchIntegration.java
/** * * @param commentID// w ww . ja v a 2 s. c o m * @param sentenceID * @param document */ public void sendToElasticSearch(String commentID, String sentenceID, String document) { try { //fix uri String url = elasticSearchURI + commentID + "_" + sentenceID; //open connection URL obj = new URL(url); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("PUT"); //append data to http packet String data = "{\"comment\": \"" + commentID + "\"," + " \"sentence\": \"" + sentenceID + "\"," + " \"document\": \"" + document + "\"}"; //write data to http socket try (OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream())) { out.write(data); out.close(); } //get response data String line = ""; try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { while ((line = in.readLine()) != null) { System.out.println(line); } } } catch (Exception e) { e.printStackTrace(); } }
From source file:net.bashtech.geobot.BotManager.java
public static String postDataLinkShortener(String postData) { URL url;// w ww. j av a 2 s.co m HttpURLConnection conn; postData = "{\"longUrl\": \"" + postData + "\"}"; try { url = new URL("https://www.googleapis.com/urlshortener/v1/url"); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "CoeBot"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length)); PrintWriter out = new PrintWriter(conn.getOutputStream()); System.out.println(postData); out.print(postData); out.close(); String response = ""; Scanner inStream = new Scanner(conn.getInputStream()); while (inStream.hasNextLine()) response += (inStream.nextLine()); inStream.close(); return response; } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return ""; }
From source file:com.wavemaker.tools.util.TomcatServer.java
public String deploy(File war, String contextRoot) { if (war == null) { throw new IllegalArgumentException("war cannot be null"); }//from w ww . j av a 2 s . c om if (!war.exists()) { throw new IllegalArgumentException("war does not exist"); } if (war.isDirectory()) { throw new IllegalArgumentException("war cannot be a directory"); } if (contextRoot == null) { contextRoot = StringUtils.fromFirstOccurrence(war.getName(), ".", -1); } contextRoot = checkContextRoot(contextRoot); if (isDeployed(contextRoot)) { undeploy(contextRoot); } String uri = getManagerUri() + "/deploy?" + getPathParam(contextRoot); HttpURLConnection con = super.getPutConnection(uri); con.setRequestProperty("Content-Type", "application/octet-stream"); con.setRequestProperty("Content-Length", String.valueOf(war.length())); prepareConnection(con); try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(war)); BufferedOutputStream bos = new BufferedOutputStream(con.getOutputStream()); IOUtils.copy(bis, bos); bis.close(); bos.close(); } catch (IOException ex) { throw new ConfigurationException(ex); } return ObjectUtils.toString(getResponse(con), ""); }
From source file:com.psbk.modulperwalian.Controller.DosenController.java
public String getDataDosenTest() throws Exception { String nip = null;/* www.j a v a 2s. c o m*/ URL url = new URL(BASE_URL + "dosen/apa/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.addRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4="); String input = "{\"id_dosen\"dos02\"}"; OutputStream os = conn.getOutputStream(); os.write(input.getBytes()); os.flush(); if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { nip = output; } return output; }
From source file:com.google.firebase.auth.migration.AuthMigrator.java
private Task<String> exchangeToken(final String legacyToken) { if (legacyToken == null) { return Tasks.forResult(null); }/* w ww .j av a2 s . co m*/ return Tasks.call(Executors.newCachedThreadPool(), new Callable<String>() { @Override public String call() throws Exception { JSONObject postBody = new JSONObject(); postBody.put("token", legacyToken); HttpURLConnection connection = (HttpURLConnection) exchangeEndpoint.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestMethod("POST"); OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream()); try { osw.write(postBody.toString()); osw.flush(); } finally { osw.close(); } int responseCode = connection.getResponseCode(); InputStream is; if (responseCode >= 400) { is = connection.getErrorStream(); } else { is = connection.getInputStream(); } try { byte[] buffer = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int numRead = 0; while ((numRead = is.read(buffer)) >= 0) { baos.write(buffer, 0, numRead); } JSONObject resultObject = new JSONObject(new String(baos.toByteArray())); if (responseCode != 200) { throw new FirebaseWebRequestException( resultObject.getJSONObject("error").getString("message"), responseCode); } return resultObject.getString("token"); } finally { is.close(); } } }); }
From source file:com.trifork.batchcopy.client.SosiUtil.java
/** * Sends a request to a given url// w w w .ja v a 2s. com * @param url service url * @param docXml the data that should be sent * @param failOnError throw exception on error? * @return The reply from the service * @throws IOException */ private String sendRequest(String url, String action, String docXml, boolean failOnError) throws IOException { HttpURLConnection uc = null; OutputStream os = null; InputStream is = null; try { URL u = new URL(url); uc = (HttpURLConnection) u.openConnection(); uc.setDoOutput(true); uc.setDoInput(true); uc.setRequestMethod("POST"); uc.setRequestProperty("SOAPAction", "\"" + action + "\""); uc.setRequestProperty("Content-Type", "text/xml; encoding=utf-8"); os = uc.getOutputStream(); IOUtils.write(docXml, os, "UTF-8"); os.flush(); if (uc.getResponseCode() != 200) { is = uc.getErrorStream(); } else { is = uc.getInputStream(); } String res = IOUtils.toString(is, "UTF-8"); if (uc.getResponseCode() != 200 && (uc.getResponseCode() != 500 || failOnError)) { throw new RuntimeException("Got unexpected response " + uc.getResponseCode() + " from " + url); } return res; } finally { if (os != null) IOUtils.closeQuietly(os); if (is != null) IOUtils.closeQuietly(is); if (uc != null) uc.disconnect(); } }