List of usage examples for java.net HttpURLConnection setUseCaches
public void setUseCaches(boolean usecaches)
From source file:net.andylizi.colormotd.Updater.java
private boolean checkUpdate() { if (file != null && file.exists()) { cancel();/*from ww w .j av a 2s . co m*/ } URL url; try { url = new URL(UPDATE_URL); } catch (MalformedURLException ex) { if (DEBUG) { ex.printStackTrace(); } return false; } try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setRequestMethod("GET"); conn.addRequestProperty("User-Agent", userAgent); if (etag != null) { conn.addRequestProperty("If-None-Match", etag); } conn.addRequestProperty("Accept", "application/json,text/plain,*/*;charset=utf-8"); conn.addRequestProperty("Accept-Encoding", "gzip"); conn.addRequestProperty("Cache-Control", "no-cache"); conn.addRequestProperty("Date", new Date().toString()); conn.addRequestProperty("Connection", "close"); conn.setDoInput(true); conn.setDoOutput(false); conn.setConnectTimeout(2000); conn.setReadTimeout(2000); conn.connect(); if (conn.getHeaderField("ETag") != null) { etag = conn.getHeaderField("ETag"); } if (DEBUG) { logger.log(Level.INFO, "DEBUG ResponseCode: {0}", conn.getResponseCode()); logger.log(Level.INFO, "DEBUG ETag: {0}", etag); } if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { return false; } else if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { return false; } BufferedReader input = null; if (conn.getContentEncoding() != null && conn.getContentEncoding().contains("gzip")) { input = new BufferedReader( new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "UTF-8"), 1); } else { input = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"), 1); } StringBuilder builder = new StringBuilder(); String buffer = null; while ((buffer = input.readLine()) != null) { builder.append(buffer); } try { JSONObject jsonObj = (JSONObject) new JSONParser().parse(builder.toString()); int build = ((Number) jsonObj.get("build")).intValue(); if (build <= ColorMOTD.buildVersion) { return false; } String version = (String) jsonObj.get("version"); String msg = (String) jsonObj.get("msg"); String download_url = (String) jsonObj.get("url"); newVersion = new NewVersion(version, build, msg, download_url); return true; } catch (ParseException ex) { if (DEBUG) { ex.printStackTrace(); } exception = ex; return false; } } catch (SocketTimeoutException ex) { return false; } catch (IOException ex) { if (DEBUG) { ex.printStackTrace(); } exception = ex; return false; } }
From source file:net.andylizi.colormotd.anim.updater.Updater.java
private boolean checkUpdate() { if (new File(Bukkit.getUpdateFolderFile(), fileName).exists()) { cancel();//from w w w . j a v a2 s . c om } URL url; try { url = new URL(UPDATE_URL); } catch (MalformedURLException ex) { if (DEBUG) { ex.printStackTrace(); } return false; } try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setRequestMethod("GET"); conn.addRequestProperty("User-Agent", USER_AGENT); if (etag != null) { conn.addRequestProperty("If-None-Match", etag); } conn.addRequestProperty("Accept", "application/json,text/plain,*/*;charset=utf-8"); conn.addRequestProperty("Accept-Encoding", "gzip"); conn.addRequestProperty("Cache-Control", "no-cache"); conn.addRequestProperty("Date", new Date().toString()); conn.addRequestProperty("Connection", "close"); conn.setDoInput(true); conn.setDoOutput(false); conn.setConnectTimeout(2000); conn.setReadTimeout(2000); conn.connect(); if (conn.getHeaderField("ETag") != null) { etag = conn.getHeaderField("ETag"); } if (DEBUG) { logger.log(Level.INFO, "DEBUG ResponseCode: {0}", conn.getResponseCode()); logger.log(Level.INFO, "DEBUG ETag: {0}", etag); } if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { return false; } else if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { return false; } BufferedReader input = null; if (conn.getContentEncoding() != null && conn.getContentEncoding().contains("gzip")) { input = new BufferedReader( new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "UTF-8"), 1); } else { input = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"), 1); } StringBuilder builder = new StringBuilder(); String buffer = null; while ((buffer = input.readLine()) != null) { builder.append(buffer); } try { JSONObject jsonObj = (JSONObject) new JSONParser().parse(builder.toString()); int build = ((Number) jsonObj.get("build")).intValue(); if (build <= buildVersion) { return false; } String version = (String) jsonObj.get("version"); String msg = (String) jsonObj.get("msg"); String download_url = (String) jsonObj.get("url"); newVersion = new NewVersion(version, build, msg, download_url); return true; } catch (ParseException ex) { if (DEBUG) { ex.printStackTrace(); } exception = ex; return false; } } catch (SocketTimeoutException ex) { return false; } catch (IOException ex) { if (DEBUG) { ex.printStackTrace(); } exception = ex; return false; } }
From source file:com.android.volley.toolbox.HurlStack.java
/** * Opens an {@link HttpURLConnection} with parameters. * @param url//from www.j a va2 s . com * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException { HttpURLConnection connection = createConnection(url); int timeoutMs = request.getTimeoutMs(); connection.setConnectTimeout(timeoutMs); connection.setReadTimeout(timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); // use caller-provided custom SslSocketFactory, if any, for HTTPS if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory); } return connection; }
From source file:br.ufjf.taverna.core.TavernaClientBase.java
protected HttpURLConnection request(String endpoint, TavernaServerMethods method, int expectedResponseCode, String acceptData, String contentType, String filePath, String putData) throws TavernaException { HttpURLConnection connection = null; try {/*from ww w . j a v a2 s. c o m*/ String uri = this.getBaseUri() + endpoint; URL url = new URL(uri); connection = (HttpURLConnection) url.openConnection(); connection.setUseCaches(false); connection.setDoOutput(true); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cache-Control", "no-cache"); String authorization = this.username + ":" + this.password; String encodedAuthorization = "Basic " + new String(Base64.encodeBase64(authorization.getBytes())); connection.setRequestProperty("Authorization", encodedAuthorization); connection.setRequestMethod(method.getMethod()); if (acceptData != null) { connection.setRequestProperty("Accept", acceptData); } if (contentType != null) { connection.setRequestProperty("Content-Type", contentType); } if (TavernaServerMethods.GET.equals(method)) { } else if (TavernaServerMethods.POST.equals(method)) { FileReader fr = new FileReader(filePath); char[] buffer = new char[1024 * 10]; int read; OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); while ((read = fr.read(buffer)) != -1) { writer.write(buffer, 0, read); } writer.flush(); writer.close(); fr.close(); } else if (TavernaServerMethods.PUT.equals(method)) { OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(putData); writer.flush(); writer.close(); } else if (TavernaServerMethods.DELETE.equals(method)) { } int responseCode = connection.getResponseCode(); if (responseCode != expectedResponseCode) { throw new TavernaException( String.format("Invalid HTTP Response Code. Expected %d, actual %d, URL %s", expectedResponseCode, responseCode, url)); } } catch (IOException ioe) { ioe.printStackTrace(); } return connection; }
From source file:lk.appzone.client.MchoiceAventuraSmsSender.java
public String excutePost(String targetURL, String username, String password, String urlParameters) throws MchoiceAventuraMessagingException { URL url;/*from w ww . j a va 2 s . c o m*/ HttpURLConnection connection = null; try { //Create connection url = new URL(targetURL); connection = (HttpURLConnection) url.openConnection(); setSdpHeaderParams(connection); setBasicAuthentication(username, password, connection); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); sendRequest(urlParameters, connection); return getResponse(connection); } catch (Exception e) { logger.error("Error occcured while sending the request", e); throw new MchoiceAventuraMessagingException("Error occcured while sending the request", e); } finally { if (connection != null) { connection.disconnect(); } } }
From source file:com.external.volley.toolbox.HurlStack.java
/** * Opens an {@link HttpURLConnection} with parameters. * @param url/*from w ww . ja va2s . c om*/ * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException { OkHttpClient okHttpClient = new OkHttpClient(); HttpURLConnection connection = new OkUrlFactory(okHttpClient).open(url); int timeoutMs = request.getTimeoutMs(); connection.setConnectTimeout(timeoutMs); connection.setReadTimeout(timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); connection.setRequestProperty("Charset", "UTF-8"); // ? connection.setRequestProperty("connection", "keep-alive"); // use caller-provided custom SslSocketFactory, if any, for HTTPS if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory); } return connection; }
From source file:com.hackerati.android.user_sdk.volley.HHurlStack.java
/** * Opens an {@link HttpURLConnection} with parameters. * /* w ww. jav a 2s . co m*/ * @param url * @return an open connection * @throws IOException */ private HttpURLConnection openConnection(final URL url, final Request<?> request) throws IOException { final HttpURLConnection connection = createConnection(url); final int timeoutMs = request.getTimeoutMs(); connection.setConnectTimeout(timeoutMs); connection.setReadTimeout(timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); // use caller-provided custom SslSocketFactory, if any, for HTTPS if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory); } return connection; }
From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java
/** * Opens an {@link java.net.HttpURLConnection} with parameters. * @param url//from ww w .j a va2 s. co m * @return an open connection * @throws java.io.IOException */ private HttpURLConnection openConnection(URL url, Request<?> request) throws IOException { HttpURLConnection connection = createConnection(url); int timeoutMs = request.getTimeoutMs(); connection.setConnectTimeout(CONNECTION_TIME_OUT_MS); connection.setReadTimeout(timeoutMs); connection.setUseCaches(false); connection.setDoInput(true); // use caller-provided custom SslSocketFactory, if any, for HTTPS if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) { ((HttpsURLConnection) connection).setSSLSocketFactory(mSslSocketFactory); } return connection; }
From source file:net.bashtech.geobot.BotManager.java
public static String postRemoteDataStrawpoll(String urlString) { String line = ""; try {// www . j a v a2 s .c o m HttpURLConnection c = (HttpURLConnection) (new URL("http://strawpoll.me/api/v2/polls") .openConnection()); c.setRequestMethod("POST"); c.setRequestProperty("Content-Type", "application/json"); c.setRequestProperty("User-Agent", "CB2"); c.setDoOutput(true); c.setDoInput(true); c.setUseCaches(false); String queryString = urlString; c.setRequestProperty("Content-Length", Integer.toString(queryString.length())); DataOutputStream wr = new DataOutputStream(c.getOutputStream()); wr.writeBytes(queryString); wr.flush(); wr.close(); Scanner inStream = new Scanner(c.getInputStream()); while (inStream.hasNextLine()) line += (inStream.nextLine()); inStream.close(); System.out.println(line); try { JSONParser parser = new JSONParser(); Object obj = parser.parse(line); JSONObject jsonObject = (JSONObject) obj; line = (Long) jsonObject.get("id") + ""; } catch (Exception e) { e.printStackTrace(); } } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return line; }