List of usage examples for java.net URLConnection getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:org.spoutcraft.launcher.util.Utils.java
public static String executePost(String targetURL, String urlParameters, JProgressBar progress) throws PermissionDeniedException { URLConnection connection = null; try {/*from www . java2s. c o m*/ URL url = new URL(targetURL); connection = url.openConnection(); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length)); connection.setRequestProperty("Content-Language", "en-US"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(10000); connection.connect(); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); return response.toString(); } catch (SocketException e) { if (e.getMessage().equalsIgnoreCase("Permission denied: connect")) { throw new PermissionDeniedException("Permission to login was denied"); } } catch (Exception e) { String message = "Login failed..."; progress.setString(message); } return null; }
From source file:BihuHttpUtil.java
/** * ? URL ??POST/* w w w. ja va 2 s . c o m*/ * * @param url * ?? URL * @param param * ?? name1=value1&name2=value2 ? * @return ?? */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // URL URLConnection conn = realUrl.openConnection(); // conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // ??POST conn.setDoOutput(true); conn.setDoInput(true); // ?URLConnection? out = new PrintWriter(conn.getOutputStream()); // ??? out.print(param); // flush? out.flush(); // BufferedReader???URL? in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("?? POST ?" + e); e.printStackTrace(); } // finally????? finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }
From source file:net.pms.util.OpenSubtitle.java
public static String postPage(URLConnection connection, String query) throws IOException { connection.setDoOutput(true);//from w ww .j a va 2 s . c om connection.setDoInput(true); connection.setUseCaches(false); connection.setDefaultUseCaches(false); connection.setRequestProperty("Content-Type", "text/xml"); connection.setRequestProperty("Content-Length", "" + query.length()); ((HttpURLConnection) connection).setRequestMethod("POST"); //LOGGER.debug("opensub query "+query); // open up the output stream of the connection if (!StringUtils.isEmpty(query)) { try (DataOutputStream output = new DataOutputStream(connection.getOutputStream())) { output.writeBytes(query); output.flush(); } } StringBuilder page; try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { page = new StringBuilder(); String str; while ((str = in.readLine()) != null) { page.append(str.trim()); page.append("\n"); } } //LOGGER.debug("opensubs result page "+page.toString()); return page.toString(); }
From source file:org.silverpeas.calendar.ServletConnector.java
/** * Send given journal headers to Silverpeas calendar importation servlet and generate a report (as * a String)./*from ww w .j av a 2 s.c o m*/ * * @param headers List of JournalHeader objects to send to Silverpeas calendar importation servlet * @return a String object containing importation results (number of items added, modified and * deleted - or an error message) */ public final String sendHeaders(List<CalendarEntry> headers) { String report = ""; try { URLConnection con = getServletConnection(); write(headers, con.getOutputStream()); InputStream instr = con.getInputStream(); report = IOUtils.toString(instr, CharEncoding.UTF_8); instr.close(); } catch (MalformedURLException e) { report = "URL de la servlet de synchronisation incorrecte."; e.printStackTrace(); } catch (IOException e) { report = "Probleme d'entree/sortie."; e.printStackTrace(); } return report; }
From source file:org.apache.servicemix.samples.cxf_osgi.Client.java
public void sendRequest() throws Exception { URLConnection connection = new URL("http://localhost:8181/cxf/HelloWorld").openConnection(); connection.setDoInput(true);/* w ww.j a va 2 s.c o m*/ connection.setDoOutput(true); OutputStream os = connection.getOutputStream(); // Post the request file. InputStream fis = getClass().getClassLoader() .getResourceAsStream("org/apache/servicemix/samples/cxf_osgi/request.xml"); IOUtils.copy(fis, os); // Read the response. InputStream is = connection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); System.out.println("the response is =====>"); System.out.println(baos.toString()); }
From source file:org.nuxeo.ecm.automation.core.operations.blob.PostBlob.java
@OperationMethod(collector = BlobCollector.class) public Blob run(Blob blob) throws IOException { URL target = new URL(url); URLConnection conn = target.openConnection(); conn.setDoOutput(true);/*from ww w. j a v a 2 s. com*/ try (InputStream in = blob.getStream(); OutputStream out = conn.getOutputStream()) { IOUtils.copy(in, out); out.flush(); } return blob; }
From source file:org.apache.servicemix.examples.cxf.Client.java
public void sendRequest() throws Exception { URLConnection connection = new URL("http://localhost:8181/cxf/HelloWorldSecurity").openConnection(); connection.setDoInput(true);/* ww w . j a va 2 s .c om*/ connection.setDoOutput(true); OutputStream os = connection.getOutputStream(); // Post the request file. InputStream fis = getClass().getClassLoader() .getResourceAsStream("org/apache/servicemix/examples/cxf/request.xml"); IOUtils.copy(fis, os); // Read the response. InputStream is = connection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); System.out.println("the response is =====>"); System.out.println(baos.toString()); }
From source file:org.apache.servicemix.examples.cxf.wsaddressing.Client.java
public void sendRequest() throws Exception { URLConnection connection = new URL("http://localhost:8181/cxf/SoapContext/SoapPort").openConnection(); connection.setDoInput(true);/*from www. j a v a 2 s . co m*/ connection.setDoOutput(true); OutputStream os = connection.getOutputStream(); // Post the request file. InputStream fis = getClass().getClassLoader() .getResourceAsStream("org/apache/servicemix/examples/cxf/wsaddressing/request.xml"); IOUtils.copy(fis, os); // Read the response. InputStream is = connection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(is, baos); System.out.println("the response is =====>"); System.out.println(baos.toString()); }
From source file:edu.stanford.muse.slant.CustomSearchHelper.java
/** returns an oauth token for the user's CSE. returns null if login failed. */ public static String authenticate(String login, String password) { //System.out.println("About to post\nURL: "+target+ "content: " + content); String authToken = null;// ww w . j a v a2s . c om String response = ""; try { URL url = new URL("https://www.google.com/accounts/ClientLogin"); URLConnection conn = url.openConnection(); // Set connection parameters. conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); // Make server believe we are form data... conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); // TODO: escape password, we'll fail if password has & // login = "musetestlogin1"; // password = "whowonthelottery"; String content = "accountType=HOSTED_OR_GOOGLE&Email=" + login + "&Passwd=" + password + "&service=cprose&source=muse.chrome.extension"; out.writeBytes(content); out.flush(); out.close(); // Read response from the input stream. BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String temp; while ((temp = in.readLine()) != null) { response += temp + "\n"; } temp = null; in.close(); log.info("Obtaining auth token: Server response:\n'" + response + "'"); String delimiter = "Auth="; /* given string will be split by the argument delimiter provided. */ String[] token = response.split(delimiter); authToken = token[1]; authToken = authToken.trim(); authToken = authToken.replaceAll("(\\r|\\n)", ""); log.info("auth token: " + authToken); return authToken; } catch (Exception e) { log.warn("Unable to authorize: " + e.getMessage()); return null; } }
From source file:net.noday.cat.web.admin.DwzManager.java
@RequestMapping(method = RequestMethod.POST) public Model gen(@RequestParam("url") String url, @RequestParam("alias") String alias, Model m) { try {// w w w . j a v a 2 s . com String urlstr = "http://dwz.cn/create.php"; URL requrl = new URL(urlstr); URLConnection conn = requrl.openConnection(); conn.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); String param = String.format(Locale.CHINA, "url=%s&alias=%s", url, alias); out.write(param); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = reader.readLine(); //{"longurl":"http:\/\/www.hao123.com\/","status":0,"tinyurl":"http:\/\/dwz.cn\/1RIKG"} ObjectMapper mapper = new ObjectMapper(); Dwz dwz = mapper.readValue(line, Dwz.class); if (dwz.getStatus() == 0) { responseData(m, dwz.getTinyurl()); } else { responseMsg(m, false, dwz.getErr_msg()); } } catch (MalformedURLException e) { log.error(e.getMessage(), e); responseMsg(m, false, e.getMessage()); } catch (IOException e) { log.error(e.getMessage(), e); responseMsg(m, false, e.getMessage()); } return m; }