public static Reader communicate(final String urlStr, final CharSequence toSend) { OutputStream outputStream = null; try { final URL url = new URL(urlStr); final URLConnection urlConnection = url.openConnection(); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); outputStream = urlConnection.getOutputStream(); final OutputStreamWriter writer = new OutputStreamWriter(outputStream, DEFAULT_CHARSET.name()); writer.append(toSend); writer.close(); return new InputStreamReader(urlConnection.getInputStream(), DEFAULT_CHARSET.name()); } catch (IOException e) { LOG.warn("exception caught", e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { LOG.warn("exception caught", e); } } } return null; }
public static CharSequence readURL(final String urlStr, final Charset charset) throws IOException { InputStream inputStream = null; try { final URL url = new URL(urlStr); final URLConnection urlConnection = url.openConnection(); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); inputStream = urlConnection.getInputStream(); return readStream(inputStream, charset); } catch (IOException ex) { LOG.warn("exception caught", ex); throw ex; } finally { if (inputStream != null) { inputStream.close(); } } }