List of usage examples for javax.net.ssl HttpsURLConnection setUseCaches
public void setUseCaches(boolean usecaches)
From source file:org.wso2.carbon.automation.test.utils.http.client.HttpsURLConnectionClient.java
public static HttpsResponse postWithBasicAuth(String uri, String requestQuery, String contentType, String userName, String password) throws IOException { if (uri.startsWith("https://")) { URL url = new URL(uri); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); String encode = new String( new org.apache.commons.codec.binary.Base64().encode((userName + ":" + password).getBytes())) .replaceAll("\n", ""); conn.setRequestProperty("Authorization", "Basic " + encode); conn.setDoOutput(true); // Triggers POST. conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("charset", "utf-8"); conn.setRequestProperty("Content-Length", "" + Integer.toString(requestQuery.getBytes().length)); conn.setUseCaches(false); conn.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; }/* ww w . j a v a 2s.co m*/ }); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(requestQuery); conn.setReadTimeout(10000); conn.connect(); System.out.println(conn.getRequestMethod()); // Get the response StringBuilder sb = new StringBuilder(); BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), Charset.defaultCharset())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } } catch (FileNotFoundException ignored) { } finally { if (rd != null) { rd.close(); } wr.flush(); wr.close(); conn.disconnect(); } return new HttpsResponse(sb.toString(), conn.getResponseCode()); } return null; }
From source file:org.wso2.carbon.automation.test.utils.http.client.HttpsURLConnectionClient.java
public static HttpsResponse putWithBasicAuth(String uri, String requestQuery, String contentType, String userName, String password) throws IOException { if (uri.startsWith("https://")) { URL url = new URL(uri); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); String encode = new String( new org.apache.commons.codec.binary.Base64().encode((userName + ":" + password).getBytes())) .replaceAll("\n", ""); ;//from w ww . ja v a2 s . c om conn.setRequestProperty("Authorization", "Basic " + encode); conn.setDoOutput(true); // Triggers POST. conn.setRequestProperty("Content-Type", contentType); conn.setRequestProperty("charset", "utf-8"); conn.setRequestProperty("Content-Length", "" + Integer.toString(requestQuery.getBytes().length)); conn.setUseCaches(false); conn.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(requestQuery); conn.setReadTimeout(10000); conn.connect(); // Get the response StringBuilder sb = new StringBuilder(); BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), Charset.defaultCharset())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } } catch (FileNotFoundException ignored) { } finally { if (rd != null) { rd.close(); } wr.flush(); wr.close(); conn.disconnect(); } return new HttpsResponse(sb.toString(), conn.getResponseCode()); } return null; }
From source file:org.wso2.carbon.automation.test.utils.http.client.HttpsURLConnectionClient.java
public static HttpsResponse postWithBasicAuth(String uri, String requestQuery, String userName, String password) throws IOException { if (uri.startsWith("https://")) { URL url = new URL(uri); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); String encode = new String( new org.apache.commons.codec.binary.Base64().encode((userName + ":" + password).getBytes())) .replaceAll("\n", ""); ;// ww w. j ava 2s. co m conn.setRequestProperty("Authorization", "Basic " + encode); conn.setDoOutput(true); // Triggers POST. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("charset", "utf-8"); conn.setRequestProperty("Content-Length", "" + Integer.toString(requestQuery.getBytes().length)); conn.setUseCaches(false); conn.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(requestQuery); conn.setReadTimeout(10000); conn.connect(); System.out.println(conn.getRequestMethod()); // Get the response StringBuilder sb = new StringBuilder(); BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), Charset.defaultCharset())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } return new HttpsResponse(sb.toString(), conn.getResponseCode()); } catch (FileNotFoundException ignored) { } finally { if (rd != null) { rd.close(); } wr.flush(); wr.close(); conn.disconnect(); } } return null; }
From source file:org.jboss.aerogear.adm.TokenService.java
/** * Returns HttpsURLConnection that 'posts' the given payload to ADM. */// ww w . j av a2 s.c o m private HttpsURLConnection post(final String payload) throws Exception { final HttpsURLConnection conn = getHttpsURLConnection(); conn.setDoOutput(true); conn.setUseCaches(false); // Set the content type . conn.setRequestProperty("content-type", APPLICATION_X_WWW_FORM_URLENCODED); conn.setRequestProperty("charset", UTF_8); conn.setRequestMethod("POST"); OutputStream out = null; final byte[] bytes = payload.getBytes(UTF_8_CHARSET); try { out = conn.getOutputStream(); out.write(bytes); out.flush(); } finally { // in case something blows up, while writing // the payload, we wanna close the stream: if (out != null) { out.close(); } } return conn; }
From source file:org.jboss.aerogear.adm.AdmService.java
/** * Returns HttpsURLConnection that 'posts' the given payload to ADM. */// w w w. j a v a 2 s . c o m private HttpsURLConnection post(final String registrationId, final String payload) throws Exception { final HttpsURLConnection conn = getHttpsURLConnection(registrationId); conn.setDoOutput(true); conn.setUseCaches(false); // Set the content type and accept headers. conn.setRequestProperty("content-type", "application/json"); conn.setRequestProperty("accept", "application/json"); conn.setRequestProperty("X-Amzn-Type-Version ", "com.amazon.device.messaging.ADMMessage@1.0"); conn.setRequestProperty("X-Amzn-Accept-Type", "com.amazon.device.messaging.ADMSendResult@1.0"); conn.setRequestMethod("POST"); // Add the authorization token as a header. conn.setRequestProperty("Authorization", "Bearer " + accessToken); OutputStream out = null; final byte[] bytes = payload.getBytes(UTF_8); try { out = conn.getOutputStream(); out.write(bytes); out.flush(); } finally { // in case something blows up, while writing // the payload, we wanna close the stream: if (out != null) { out.close(); } } return conn; }
From source file:com.google.android.apps.picview.request.CachedWebRequestFetcher.java
/** * Fetches the given URL from the web.// www .j a v a 2s . co m */ public String fetchFromWeb(URL url) { Log.d(TAG, "Fetching from web: " + url.toString()); HttpsURLConnection conn = null; HttpResponse resp = null; try { conn = (HttpsURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setReadTimeout(30000); // 30 seconds. conn.setDoInput(true); conn.addRequestProperty("GData-Version", "2"); conn.connect(); InputStream is = conn.getInputStream(); return readStringFromStream(is); } catch (Exception e) { Log.v(TAG, readStringFromStream(conn.getErrorStream())); e.printStackTrace(); } return null; }
From source file:com.nadmm.airports.notams.NotamService.java
private void fetchNotams(String icaoCode, File notamFile) throws IOException { String params = String.format(NOTAM_PARAM, icaoCode); HttpsURLConnection conn = (HttpsURLConnection) NOTAM_URL.openConnection(); conn.setRequestProperty("Connection", "close"); conn.setDoInput(true);/* w w w .jav a 2s . c o m*/ conn.setDoOutput(true); conn.setUseCaches(false); conn.setConnectTimeout(30 * 1000); conn.setReadTimeout(30 * 1000); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US)"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", Integer.toString(params.length())); // Write out the form parameters as the request body OutputStream faa = conn.getOutputStream(); faa.write(params.getBytes("UTF-8")); faa.close(); int response = conn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { // Request was successful, parse the html to extract notams InputStream in = conn.getInputStream(); ArrayList<String> notams = parseNotamsFromHtml(in); in.close(); // Write the NOTAMS to the cache file BufferedOutputStream cache = new BufferedOutputStream(new FileOutputStream(notamFile)); for (String notam : notams) { cache.write(notam.getBytes()); cache.write('\n'); } cache.close(); } }
From source file:com.glaf.core.util.http.HttpUtils.java
/** * ?https?/*from w w w . ja va 2s . c om*/ * * @param requestUrl * ? * @param method * ?GET?POST * @param content * ??? * @return */ public static String doRequest(String requestUrl, String method, String content, boolean isSSL) { log.debug("requestUrl:" + requestUrl); HttpsURLConnection conn = null; InputStream inputStream = null; BufferedReader bufferedReader = null; InputStreamReader inputStreamReader = null; StringBuffer buffer = new StringBuffer(); try { URL url = new URL(requestUrl); conn = (HttpsURLConnection) url.openConnection(); if (isSSL) { // SSLContext?? TrustManager[] tm = { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); // SSLContextSSLSocketFactory SSLSocketFactory ssf = sslContext.getSocketFactory(); conn.setSSLSocketFactory(ssf); } conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); // ?GET/POST conn.setRequestMethod(method); if ("GET".equalsIgnoreCase(method)) { conn.connect(); } // ???? if (StringUtils.isNotEmpty(content)) { OutputStream outputStream = conn.getOutputStream(); // ???? outputStream.write(content.getBytes("UTF-8")); outputStream.flush(); outputStream.close(); } // ??? inputStream = conn.getInputStream(); inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); bufferedReader = new BufferedReader(inputStreamReader); String str = null; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } log.debug("response:" + buffer.toString()); } catch (ConnectException ce) { ce.printStackTrace(); log.error(" http server connection timed out."); } catch (Exception ex) { ex.printStackTrace(); log.error("http request error:{}", ex); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(bufferedReader); IOUtils.closeQuietly(inputStreamReader); if (conn != null) { conn.disconnect(); } } return buffer.toString(); }
From source file:org.openhab.binding.neato.internal.VendorVorwerk.java
@Override public String executeRequest(String httpMethod, String url, Properties httpHeaders, InputStream content, String contentType, int timeout) throws IOException { URL requestUrl = new URL(url); HttpsURLConnection connection = (HttpsURLConnection) requestUrl.openConnection(); applyNucleoSslConfiguration(connection); connection.setRequestMethod(httpMethod); for (String propName : httpHeaders.stringPropertyNames()) { connection.addRequestProperty(propName, httpHeaders.getProperty(propName)); }/* ww w .j ava 2 s .com*/ connection.setUseCaches(false); connection.setDoOutput(true); connection.setConnectTimeout(10000); connection.setReadTimeout(10000); content.reset(); IOUtils.copy(content, connection.getOutputStream()); java.io.InputStream is = connection.getInputStream(); return IOUtils.toString(is); }
From source file:com.produban.cloudfoundry.bosh.bosh_javaclient.BoshClientImpl.java
/** * This method opens a {@link HttpsURLConnection} to the BOSH director using * the given path and sets up 'GET' method and authentication. * * @param path/*from ww w . java 2s.co m*/ * the path (starting with '/') * @return the {@link HttpsURLConnection} object * @throws MalformedURLException * @throws IOException * @throws ProtocolException * @throws UnsupportedEncodingException */ private HttpsURLConnection setupHttpsUrlConnection(String path) throws MalformedURLException, IOException, ProtocolException, UnsupportedEncodingException { URL requestURL = new URL("https://" + this.host + ":" + this.port + path); HttpsURLConnection httpsURLConnection = (HttpsURLConnection) requestURL.openConnection(); httpsURLConnection.setRequestMethod("GET"); httpsURLConnection.setUseCaches(false); String credentials = this.username + ":" + this.password; String basicAuthHeaderValue = "Basic " + Base64.encodeBase64String(credentials.getBytes("UTF-8")); httpsURLConnection.setRequestProperty("Authorization", basicAuthHeaderValue); httpsURLConnection.setDoInput(true); return httpsURLConnection; }