List of usage examples for java.net URLConnection setDoOutput
public void setDoOutput(boolean dooutput)
From source file:io.v.positioning.gae.ServletPostAsyncTask.java
@Override protected String doInBackground(Context... params) { mContext = params[0];//from w w w .j ava 2 s .c om DataOutputStream os = null; InputStream is = null; try { URLConnection conn = mUrl.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.connect(); os = new DataOutputStream(conn.getOutputStream()); os.write(mData.toString().getBytes("UTF-8")); is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); return br.readLine(); } catch (IOException e) { return "IOException while contacting GEA: " + e.getMessage(); } catch (Exception e) { return "Exception while contacting GEA: " + e.getLocalizedMessage(); } finally { if (os != null) try { os.close(); } catch (IOException e) { return "IOException closing os: " + e.getMessage(); } if (is != null) try { is.close(); } catch (IOException e) { return "IOException closing is: " + e.getMessage(); } } }
From source file:com.glaf.base.modules.todo.job.TodoQuartzJob.java
public void sendMessageToAllUsersViaJSP() { try {//from w ww. j a v a 2 s. c o m if (sendMessageServiceUrl != null) { URL url = new URL(sendMessageServiceUrl); URLConnection con = url.openConnection(); con.setDoOutput(true); InputStream in = con.getInputStream(); in = new BufferedInputStream(in); Reader r = new InputStreamReader(in); int c; logger.debug("==============Beging===================="); while ((c = r.read()) != -1) { logger.debug((char) c); } in.close(); logger.debug("===============End======================"); } } catch (Exception ex) { ex.printStackTrace(); logger.error(ex); } }
From source file:util.connection.AnonymousClient.java
public String download(URL source) throws IOException { // set the request URL URL request = new URL(source, source.getFile(), handler); // send request and receive response log.info("download (start) from source=" + source); URLConnection connection = request.openConnection(); connection.setDoOutput(true); connection.setDoInput(true);/* ww w . java 2 s . c o m*/ connection.setUseCaches(false); connection.setAllowUserInteraction(false); connection.connect(); String responsebody = IOUtils.toString(connection.getInputStream(), "UTF-8"); // read the response netLayer.clear(); netLayer.waitUntilReady(); return responsebody; }
From source file:org.jabsorb.client.URLConnectionSession.java
public JSONObject sendAndReceive(JSONObject message) { try {/*from w w w . j av a 2 s. c om*/ URLConnection connection = url.openConnection(); connection.setDoOutput(true); // As per http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html Writer request = new OutputStreamWriter(connection.getOutputStream()); request.write(message.toString()); request.close(); // TODO the following sequence of reading a string out of output stream is too complicated // there must be a simpler way StringBuffer builder = new StringBuffer(1024); char[] buffer = new char[1024]; Reader reader = new InputStreamReader(connection.getInputStream()); while (true) { int bytesRead = reader.read(buffer); if (bytesRead < 0) break; builder.append(buffer, 0, bytesRead); } reader.close(); JSONTokener tokener = new JSONTokener(builder.toString()); Object rawResponseMessage = tokener.nextValue(); JSONObject responseMessage = (JSONObject) rawResponseMessage; if (responseMessage == null) throw new ClientError("Invalid response type - " + rawResponseMessage.getClass()); return responseMessage; } catch (IOException ex) { throw new ClientError(ex); } catch (JSONException ex) { throw new ClientError(ex); } }
From source file:org.sleeksnap.uploaders.url.GoogleShortener.java
@Override public String upload(final URLUpload url) throws Exception { // Sanity check, otherwise google's api returns a 400 if (url.toString().matches("http://goo.gl/[a-zA-Z0-9]{1,10}")) { return url.toString(); }/*from www. j a va2 s . co m*/ final URLConnection connection = new URL(PAGE_URL).openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "application/json"); final JSONObject out = new JSONObject(); out.put("longUrl", url.getURL()); final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(out.toString()); writer.flush(); writer.close(); final String contents = StreamUtils.readContents(connection.getInputStream()); final JSONObject resp = new JSONObject(contents); if (resp.has("id")) { return resp.getString("id"); } else { throw new UploadException("Unable to find short url"); } }
From source file:org.apache.roller.weblogger.ui.rendering.plugins.comments.AkismetCommentValidator.java
public int validate(WeblogEntryComment comment, RollerMessages messages) { StringBuffer sb = new StringBuffer(); sb.append("blog=").append(WebloggerFactory.getWeblogger().getUrlStrategy() .getWeblogURL(comment.getWeblogEntry().getWebsite(), null, true)).append("&"); sb.append("user_ip=").append(comment.getRemoteHost()).append("&"); sb.append("user_agent=").append(comment.getUserAgent()).append("&"); sb.append("referrer=").append(comment.getReferrer()).append("&"); sb.append("permalink=").append(comment.getWeblogEntry().getPermalink()).append("&"); sb.append("comment_type=").append("comment").append("&"); sb.append("comment_author=").append(comment.getName()).append("&"); sb.append("comment_author_email=").append(comment.getEmail()).append("&"); sb.append("comment_author_url=").append(comment.getUrl()).append("&"); sb.append("comment_content=").append(comment.getContent()); try {//w w w. ja va 2s .c om URL url = new URL("http://" + apikey + ".rest.akismet.com/1.1/comment-check"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setRequestProperty("User_Agent", "Roller " + WebloggerFactory.getWeblogger().getVersion()); conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=utf8"); conn.setRequestProperty("Content-length", Integer.toString(sb.length())); OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream()); osr.write(sb.toString(), 0, sb.length()); osr.flush(); osr.close(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String response = br.readLine(); if ("true".equals(response)) { messages.addError("comment.validator.akismetMessage"); return 0; } else return 100; } catch (Exception e) { log.error("ERROR checking comment against Akismet", e); } return 0; // interpret error as spam: better safe than sorry? }
From source file:org.jabsorb.ng.client.URLConnectionSession.java
public JSONObject sendAndReceive(JSONObject message) { try {/*w ww . j ava2s . c o m*/ URLConnection connection = url.openConnection(); connection.setDoOutput(true); // As per // http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html Writer request = new OutputStreamWriter(connection.getOutputStream()); request.write(message.toString()); request.close(); // TODO the following sequence of reading a string out of output // stream is too complicated // there must be a simpler way StringBuffer builder = new StringBuffer(1024); char[] buffer = new char[1024]; Reader reader = new InputStreamReader(connection.getInputStream()); while (true) { int bytesRead = reader.read(buffer); if (bytesRead < 0) break; builder.append(buffer, 0, bytesRead); } reader.close(); JSONTokener tokener = new JSONTokener(builder.toString()); Object rawResponseMessage = tokener.nextValue(); JSONObject responseMessage = (JSONObject) rawResponseMessage; if (responseMessage == null) throw new ClientError("Invalid response type - " + rawResponseMessage.getClass()); return responseMessage; } catch (IOException ex) { throw new ClientError(ex); } catch (JSONException ex) { throw new ClientError(ex); } }
From source file:org.apache.axis2.transport.testkit.http.JavaNetClient.java
public void sendMessage(ClientOptions options, ContentType contentType, byte[] message) throws Exception { URL url = new URL(channel.getEndpointReference().getAddress()); log.debug("Opening connection to " + url + " using " + URLConnection.class.getName()); try {//from ww w .j a v a 2 s . com URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", contentType.toString()); if (contentType.getBaseType().equals("text/xml")) { connection.setRequestProperty("SOAPAction", ""); } OutputStream out = connection.getOutputStream(); out.write(message); out.close(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; log.debug("Response code: " + httpConnection.getResponseCode()); log.debug("Response message: " + httpConnection.getResponseMessage()); int i = 0; String headerValue; while ((headerValue = httpConnection.getHeaderField(i)) != null) { String headerName = httpConnection.getHeaderFieldKey(i); if (headerName != null) { log.debug(headerName + ": " + headerValue); } else { log.debug(headerValue); } i++; } } InputStream in = connection.getInputStream(); IOUtils.copy(in, System.out); in.close(); } catch (IOException ex) { log.debug("Got exception", ex); throw ex; } }
From source file:org.talend.mdm.commmon.util.datamodel.management.SecurityEntityResolver.java
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (systemId != null) { Pattern httpUrl = Pattern.compile("(http|https|ftp):(\\//|\\\\)(.*):(.*)"); //$NON-NLS-1$ Matcher match = httpUrl.matcher(systemId); if (match.matches()) { StringBuilder buffer = new StringBuilder(); String credentials = Base64.encodeBase64String("admin:talend".getBytes()); //$NON-NLS-1$ URL url = new URL(systemId); URLConnection conn = url.openConnection(); conn.setAllowUserInteraction(true); conn.setDoOutput(true); conn.setDoInput(true);/*from w ww . j a v a2 s . co m*/ conn.setRequestProperty("Authorization", "Basic " + credentials); //$NON-NLS-1$ conn.setRequestProperty("Expect", "100-continue"); //$NON-NLS-1$ InputStreamReader doc = new InputStreamReader(conn.getInputStream()); BufferedReader reader = new BufferedReader(doc); String line = reader.readLine(); while (line != null) { buffer.append(line); line = reader.readLine(); } return new InputSource(new StringReader(buffer.toString())); } else { int mark = systemId.indexOf("file:///"); //$NON-NLS-1$ String path = systemId.substring((mark != -1 ? mark + "file:///".length() : 0)); //$NON-NLS-1$ File file = new File(path); return new InputSource(file.toURL().openStream()); } } return null; }
From source file:com.amalto.core.util.SecurityEntityResolver.java
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (systemId != null) { Pattern httpUrl = Pattern.compile("(http|https|ftp):(\\//|\\\\)(.*):(.*)"); Matcher match = httpUrl.matcher(systemId); if (match.matches()) { StringBuilder buffer = new StringBuilder(); String credentials = new String(Base64.encodeBase64("admin:talend".getBytes())); URL url = new URL(systemId); URLConnection conn = url.openConnection(); conn.setAllowUserInteraction(true); conn.setDoOutput(true); conn.setDoInput(true);// w w w. j a v a 2 s. c o m conn.setRequestProperty("Authorization", "Basic " + credentials); conn.setRequestProperty("Expect", "100-continue"); InputStreamReader doc = new InputStreamReader(conn.getInputStream()); BufferedReader reader = new BufferedReader(doc); String line = reader.readLine(); while (line != null) { buffer.append(line); line = reader.readLine(); } return new InputSource(new StringReader(buffer.toString())); } else { int mark = systemId.indexOf("file:///"); String path = systemId.substring((mark != -1 ? mark + "file:///".length() : 0)); File file = new File(path); return new InputSource(file.toURL().openStream()); } } return null; }