List of usage examples for java.io OutputStreamWriter close
public void close() throws IOException
From source file:Main.java
public static String request(String url, Map<String, String> cookies, Map<String, String> parameters) throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"); if (cookies != null && !cookies.isEmpty()) { StringBuilder cookieHeader = new StringBuilder(); for (String cookie : cookies.values()) { if (cookieHeader.length() > 0) { cookieHeader.append(";"); }//from w w w. j a v a 2s .c om cookieHeader.append(cookie); } connection.setRequestProperty("Cookie", cookieHeader.toString()); } connection.setDoInput(true); if (parameters != null && !parameters.isEmpty()) { connection.setDoOutput(true); OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream()); osw.write(parametersToWWWFormURLEncoded(parameters)); osw.flush(); osw.close(); } if (cookies != null) { for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) { if (headerEntry != null && headerEntry.getKey() != null && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) { for (String header : headerEntry.getValue()) { for (HttpCookie httpCookie : HttpCookie.parse(header)) { cookies.put(httpCookie.getName(), httpCookie.toString()); } } } } } Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringWriter w = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while ((n = r.read(buffer)) != -1) { w.write(buffer, 0, n); } r.close(); return w.toString(); }
From source file:ilearnrw.utils.ServerHelperClass.java
public static String sendPost(String link, String urlParameters) throws Exception { String url = baseUrl + link;//from w w w . j ava2 s . c o m URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Authorization", userNamePasswordBase64("api", "api")); con.setRequestProperty("Content-Type", "application/json;charset=utf-8"); con.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF8"); wr.write(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); }
From source file:com.company.project.core.connector.HttpClientHelper.java
public static String getResponseStringFromConn(HttpURLConnection conn, String payLoad) throws IOException { // Send the http message payload to the server. if (payLoad != null) { conn.setDoOutput(true);/*from w ww . j ava 2s . co m*/ OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream()); osw.write(payLoad); osw.flush(); osw.close(); } // Get the message response from the server. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = ""; StringBuffer stringBuffer = new StringBuffer(); while ((line = br.readLine()) != null) { stringBuffer.append(line); } br.close(); return stringBuffer.toString(); }
From source file:com.beginner.core.utils.SmsUtil.java
public static String SMS(String postData, String postUrl) { try {//from w ww . j a va 2s . c o m //??POST URL url = new URL(postUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setUseCaches(false); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", "" + postData.length()); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(postData); out.flush(); out.close(); //??? if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { System.out.println("connect failed!"); return ""; } //?? String line, result = ""; BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); while ((line = in.readLine()) != null) { result += line + "\n"; } in.close(); return result; } catch (IOException e) { e.printStackTrace(System.out); } return ""; }
From source file:eu.swiec.bearballin.common.io.FileIO.java
@Deprecated public static void writeStringtoFileSource(String fileName, String stringToWrite, String encoding) throws IOException { File outputFile = new File(fileName); OutputStream outStream = new FileOutputStream(outputFile); OutputStreamWriter osw = new OutputStreamWriter(outStream, encoding); osw.write(stringToWrite);/*from w w w . j a v a 2 s . c o m*/ osw.close(); outStream.close(); }
From source file:com.jiubang.core.util.HttpUtils.java
/** * Send an HTTP(s) request with POST parameters. * /* w w w .j a v a2 s. c o m*/ * @param parameters * @param url * @throws UnsupportedEncodingException * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException */ static void doPost(Map<?, ?> parameters, URL url) throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException { URLConnection cnx = getConnection(url); // Construct data StringBuilder dataBfr = new StringBuilder(); Iterator<?> iKeys = parameters.keySet().iterator(); while (iKeys.hasNext()) { if (dataBfr.length() != 0) { dataBfr.append('&'); } String key = (String) iKeys.next(); dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=') .append(URLEncoder.encode((String) parameters.get(key), "UTF-8")); } // POST data cnx.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream()); Loger.d(LOG_TAG, "Posting crash report data"); wr.write(dataBfr.toString()); wr.flush(); wr.close(); Loger.d(LOG_TAG, "Reading response"); BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream())); String line; while ((line = rd.readLine()) != null) { Loger.d(LOG_TAG, line); } rd.close(); }
From source file:org.acra.HttpUtils.java
/** * Send an HTTP(s) request with POST parameters. * //from ww w. ja va2 s . c om * @param parameters * @param url * @throws UnsupportedEncodingException * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException */ static void doPost(Map<?, ?> parameters, URL url) throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException { URLConnection cnx = getConnection(url); // Construct data StringBuilder dataBfr = new StringBuilder(); Iterator<?> iKeys = parameters.keySet().iterator(); while (iKeys.hasNext()) { if (dataBfr.length() != 0) { dataBfr.append('&'); } String key = (String) iKeys.next(); dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=') .append(URLEncoder.encode((String) parameters.get(key), "UTF-8")); } // POST data cnx.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream()); Log.d(LOG_TAG, "Posting crash report data"); wr.write(dataBfr.toString()); wr.flush(); wr.close(); Log.d(LOG_TAG, "Reading response"); BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream())); String line; while ((line = rd.readLine()) != null) { Log.d(LOG_TAG, line); } rd.close(); }
From source file:de.innovationgate.utils.XStreamUtils.java
/** * Serializes an object to a UTF-8 encoded output stream * @param obj The object to write//from ww w . ja v a 2 s . co m * @param xstream The XStream instance to use * @param out The output stream to write to * @throws IOException */ public static void writeUtf8ToOutputStream(Object obj, XStream xstream, OutputStream out) throws IOException { BufferedOutputStream bufOut = new BufferedOutputStream(out); OutputStreamWriter writer; try { writer = new OutputStreamWriter(bufOut, "UTF-8"); xstream.toXML(obj, writer); writer.flush(); writer.close(); } catch (UnsupportedEncodingException e) { // Cannot happen since Java always supports UTF-8 } }
From source file:com.ct855.util.HttpsClientUtil.java
public static String postUrl(String url, Map<String, String> params) throws IOException, NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException { //SSLContext?? TrustManager[] trustAllCerts = new TrustManager[] { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); //SSLContextSSLSocketFactory SSLSocketFactory ssf = sslContext.getSocketFactory(); String data = ""; for (String key : params.keySet()) { data += "&" + URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(params.get(key), "UTF-8"); }// w ww . j a v a 2 s.co m data = data.substring(1); System.out.println("postUrl=>data:" + data); URL aURL = new java.net.URL(url); HttpsURLConnection aConnection = (HttpsURLConnection) aURL.openConnection(); aConnection.setSSLSocketFactory(ssf); aConnection.setDoOutput(true); aConnection.setDoInput(true); aConnection.setRequestMethod("POST"); OutputStreamWriter streamToAuthorize = new java.io.OutputStreamWriter(aConnection.getOutputStream()); streamToAuthorize.write(data); streamToAuthorize.flush(); streamToAuthorize.close(); InputStream resultStream = aConnection.getInputStream(); BufferedReader aReader = new java.io.BufferedReader(new java.io.InputStreamReader(resultStream)); StringBuffer aResponse = new StringBuffer(); String aLine = aReader.readLine(); while (aLine != null) { aResponse.append(aLine + "\n"); aLine = aReader.readLine(); } resultStream.close(); return aResponse.toString(); }
From source file:com.wareninja.opensource.common.wsrequest.HttpUtils.java
/** * Send an HTTP(s) request with POST parameters. * //from w w w. ja va 2 s. co m * @param parameters * @param url * @throws UnsupportedEncodingException * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException */ public static void doPost(Map<?, ?> parameters, URL url) throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException { URLConnection cnx = getConnection(url); // Construct data StringBuilder dataBfr = new StringBuilder(); Iterator<?> iKeys = parameters.keySet().iterator(); while (iKeys.hasNext()) { if (dataBfr.length() != 0) { dataBfr.append('&'); } String key = (String) iKeys.next(); dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=') .append(URLEncoder.encode((String) parameters.get(key), "UTF-8")); } // POST data cnx.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream()); if (LOGGING.DEBUG) Log.d(LOG_TAG, "Posting crash report data"); wr.write(dataBfr.toString()); wr.flush(); wr.close(); if (LOGGING.DEBUG) Log.d(LOG_TAG, "Reading response"); BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream())); String line; int linecount = 0; while ((line = rd.readLine()) != null) { linecount++; if (linecount <= 2) { if (LOGGING.DEBUG) Log.d(LOG_TAG, line); } } rd.close(); }