java.net.HttpURLConnection.setUseCaches()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(226)

本文整理了Java中java.net.HttpURLConnection.setUseCaches()方法的一些代码示例,展示了HttpURLConnection.setUseCaches()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpURLConnection.setUseCaches()方法的具体详情如下:
包路径:java.net.HttpURLConnection
类名称:HttpURLConnection
方法名:setUseCaches

HttpURLConnection.setUseCaches介绍

暂无

代码示例

代码示例来源:origin: libgdx/libgdx

/** Downloads the content of the specified url to the array. The array has to be big enough. */
private int download (byte[] out, String url) {
  InputStream in = null;
  try {
    HttpURLConnection conn = null;
    conn = (HttpURLConnection)new URL(url).openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(false);
    conn.setUseCaches(true);
    conn.connect();
    in = conn.getInputStream();
    int readBytes = 0;
    while (true) {
      int length = in.read(out, readBytes, out.length - readBytes);
      if (length == -1) break;
      readBytes += length;
    }
    return readBytes;
  } catch (Exception ex) {
    return 0;
  } finally {
    StreamUtils.closeQuietly(in);
  }
}

代码示例来源:origin: pwittchen/ReactiveNetwork

protected HttpURLConnection createHttpUrlConnection(final String host, final int port,
   final int timeoutInMs) throws IOException {
  URL initialUrl = new URL(host);
  URL url = new URL(initialUrl.getProtocol(), initialUrl.getHost(), port, initialUrl.getFile());
  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  urlConnection.setConnectTimeout(timeoutInMs);
  urlConnection.setReadTimeout(timeoutInMs);
  urlConnection.setInstanceFollowRedirects(false);
  urlConnection.setUseCaches(false);
  return urlConnection;
 }
}

代码示例来源:origin: plantuml/plantuml

public static int extractDownloadableVersion(String host, String port) {
  if (host != null && port != null) {
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port);
  }
  try {
    final URL url = new URL("http://plantuml.com/download");
    final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setUseCaches(false);
    urlConnection.connect();
    if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
      final BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
      final int lastversion = extractVersion(in);
      in.close();
      urlConnection.disconnect();
      return lastversion;
    }
  } catch (IOException e) {
    Log.error(e.toString());
  }
  return -1;
}

代码示例来源:origin: Dreampie/Resty

/**
 * outConnection
 *
 * @param httpMethod
 * @param httpClientRequest
 * @return
 * @throws IOException
 */
private HttpURLConnection getStreamConnection(String httpMethod, HttpClientRequest httpClientRequest) throws IOException {
 URL url;
 HttpURLConnection conn;
 url = new URL(apiUrl + httpClientRequest.getEncodedRestPath());
 conn = openHttpURLConnection(url, httpClientRequest, httpMethod);
 conn.setDoOutput(true);
 conn.setUseCaches(false);
 return conn;
}

代码示例来源:origin: geotools/geotools

return null;
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(downloadTimeout);
connection.setReadTimeout(downloadTimeout);
connection.setUseCaches(false);
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
List<byte[]> blocks = new LinkedList<byte[]>();
try {
  input = connection.getInputStream();
  while (true) {
    byte[] block = new byte[blockSize];

代码示例来源:origin: org.apache.hadoop/hadoop-common

throw new IOException(ex);
conn.setUseCaches(false);
conn.setRequestMethod(method);
if (method.equals(HTTP_POST) || method.equals(HTTP_PUT)) {
 conn.setDoOutput(true);

代码示例来源:origin: ctripcorp/apollo

/**
 * ping the url, return true if ping ok, false otherwise
 */
public static boolean pingUrl(String address) {
 try {
  URL urlObj = new URL(address);
  HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
  connection.setRequestMethod("GET");
  connection.setUseCaches(false);
  connection.setConnectTimeout(DEFAULT_TIMEOUT_IN_SECONDS);
  connection.setReadTimeout(DEFAULT_TIMEOUT_IN_SECONDS);
  int statusCode = connection.getResponseCode();
  cleanUpConnection(connection);
  return (200 <= statusCode && statusCode <= 399);
 } catch (Throwable ignore) {
 }
 return false;
}

代码示例来源:origin: hs-web/hsweb-framework

url = new URL( urlStr );
connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches( true );
String physicalPath = this.rootPath + savePath;
State state = StorageManager.saveFileByInputStream( connection.getInputStream(), physicalPath );

代码示例来源:origin: openmrs/openmrs-core

/**
 * Tests the connection to the specified URL
 *
 * @param urlString the url to test
 * @return true if a connection a established otherwise false
 */
protected static boolean testConnection(String urlString) {
  try {
    HttpURLConnection urlConnect = (HttpURLConnection) new URL(urlString).openConnection();
    //wait for 15sec
    urlConnect.setConnectTimeout(15000);
    urlConnect.setUseCaches(false);
    //trying to retrieve data from the source. If there
    //is no connection, this line will fail
    urlConnect.getContent();
    return true;
  }
  catch (IOException e) {
    if (log.isDebugEnabled()) {
      log.debug("Error generated:", e);
    }
  }
  
  return false;
}

代码示例来源:origin: org.apache.cxf/cxf-rt-transports-http

protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException {
  HttpURLConnection connection = createConnection(message, address, csPolicy);
  connection.setDoOutput(true);
  connection.setReadTimeout(rtimeout);
  connection.setUseCaches(false);
    connection.setRequestMethod(httpRequestMethod);
  } catch (java.net.ProtocolException ex) {
    boolean b = MessageUtils.getContextualBoolean(message,

代码示例来源:origin: alipay/sofa-rpc

private HttpURLConnection createConnection(URL url, String method, boolean doOutput) {
  HttpURLConnection con;
  try {
    con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod(method);
    con.setConnectTimeout(connectTimeout);
    con.setReadTimeout(readTimeout);
    con.setDoOutput(doOutput);
    con.setDoInput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type", "text/plain");
    return con;
  } catch (IOException e) {
    e.printStackTrace();
    return null;
  }
}

代码示例来源:origin: alipay/sofa-rpc

private HttpURLConnection createConnection(URL url, String method, boolean doOutput) {
  HttpURLConnection con;
  try {
    con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod(method);
    con.setConnectTimeout(connectTimeout);
    con.setReadTimeout(readTimeout);
    con.setDoOutput(doOutput);
    con.setDoInput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type", "text/plain");
    return con;
  } catch (IOException e) {
    e.printStackTrace();
    return null;
  }
}

代码示例来源:origin: languagetool-org/languagetool

@NotNull
private HttpURLConnection postToServer(String json, URL url) throws IOException {
 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 conn.setRequestMethod("POST");
 conn.setUseCaches(false);
 conn.setRequestProperty("Content-Type", "application/json");
 conn.setDoOutput(true);
 try {
  try (DataOutputStream dos = new DataOutputStream(conn.getOutputStream())) {
   //System.out.println("POSTING: " + json);
   dos.write(json.getBytes("utf-8"));
  }
 } catch (IOException e) {
  throw new RuntimeException("Could not connect OpenNMT server at " + url);
 }
 return conn;
}

代码示例来源:origin: graphhopper/graphhopper

public HttpURLConnection createConnection(String urlStr) throws IOException {
  URL url = new URL(urlStr);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  // Will yield in a POST request: conn.setDoOutput(true);
  conn.setDoInput(true);
  conn.setUseCaches(true);
  conn.setRequestProperty("Referrer", referrer);
  conn.setRequestProperty("User-Agent", userAgent);
  // suggest respond to be gzipped or deflated (which is just another compression)
  // http://stackoverflow.com/q/3932117
  conn.setRequestProperty("Accept-Encoding", acceptEncoding);
  conn.setReadTimeout(timeout);
  conn.setConnectTimeout(timeout);
  return conn;
}

代码示例来源:origin: jmdhappy/xxpay-master

/**
 * 设置http请求默认属性
 *
 * @param httpConnection
 */
protected void setHttpRequest(HttpURLConnection httpConnection) {
  //设置连接超时时间
  httpConnection.setConnectTimeout(this.timeOut * 1000);
  //User-Agent
  httpConnection.setRequestProperty("User-Agent",
      HttpClient.USER_AGENT_VALUE);
  //不使用缓存
  httpConnection.setUseCaches(false);
  //允许输入输出
  httpConnection.setDoInput(true);
  httpConnection.setDoOutput(true);
}

代码示例来源:origin: gotev/android-upload-service

public HurlStackConnection(String method, String url, boolean followRedirects,
              boolean useCaches, int connectTimeout, int readTimeout)
    throws IOException {
  Logger.debug(getClass().getSimpleName(), "creating new connection");
  URL urlObj = new URL(url);
  if (urlObj.getProtocol().equals("https")) {
    mConnection = (HttpsURLConnection) urlObj.openConnection();
  } else {
    mConnection = (HttpURLConnection) urlObj.openConnection();
  }
  mConnection.setDoInput(true);
  mConnection.setDoOutput(true);
  mConnection.setConnectTimeout(connectTimeout);
  mConnection.setReadTimeout(readTimeout);
  mConnection.setUseCaches(useCaches);
  mConnection.setInstanceFollowRedirects(followRedirects);
  mConnection.setRequestMethod(method);
}

代码示例来源:origin: alibaba/mdrill

public void Request(StringBuffer urlFormat ) throws IOException
{
  URL u = new URL("http://mon.alibaba-inc.com/noticenter/send.do");
  HttpURLConnection con = (HttpURLConnection) u.openConnection();
  con.setRequestMethod("POST");
  con.setDoOutput(true);
  con.setDoInput(true);
  con.setUseCaches(false);
  con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(),"UTF-8");
  osw.write(urlFormat.toString());
  osw.flush();
  osw.close();
  
  InputStreamReader urlStream = new InputStreamReader(con.getInputStream(), "utf-8");
  BufferedReader in = new BufferedReader(urlStream);
  int bufferlen = 1;
  char[] buffer = new char[bufferlen];
  int readBytes = 0;
  while (true) {
      readBytes = in.read(buffer, 0, bufferlen);
      if (readBytes < 0) {
          break;
      }
   }
  in.close();
  urlStream.close();
  con.disconnect();
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

private InputStream readData(int offset, int length) throws IOException{
  HttpURLConnection conn = (HttpURLConnection) zipUrl.openConnection();
  conn.setDoOutput(false);
  conn.setUseCaches(false);
  conn.setInstanceFollowRedirects(false);
  String range = "-";
  if (offset != Integer.MAX_VALUE){
    range = offset + range;
  }
  if (length != Integer.MAX_VALUE){
    if (offset != Integer.MAX_VALUE){
      range = range + (offset + length - 1);
    }else{
      range = range + length;
    }
  }
  conn.setRequestProperty("Range", "bytes=" + range);
  conn.connect();
  if (conn.getResponseCode() == HttpURLConnection.HTTP_PARTIAL){
    return conn.getInputStream();
  }else if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){
    throw new IOException("Your server does not support HTTP feature Content-Range. Please contact your server administrator.");
  }else{
    throw new IOException(conn.getResponseCode() + " " + conn.getResponseMessage());
  }
}

代码示例来源:origin: javamelody/javamelody

void checkForUpdate() throws IOException {
  final String anonymousData = getAnonymousData();
  final HttpURLConnection connection = (HttpURLConnection) new URL(serverUrl)
      .openConnection();
  connection.setUseCaches(false);
  connection.setDoOutput(true);
  connection.setRequestMethod("POST");
  connection.setConnectTimeout(60000);
  connection.setReadTimeout(60000);
  connection.setRequestProperty("data", anonymousData);
  connection.connect();
  final Properties properties = new Properties();
  final InputStream input = connection.getInputStream();
  try {
    properties.load(input);
  } finally {
    input.close();
  }
  final String javamelodyVersion = properties.getProperty("version");
  if (javamelodyVersion != null && Parameters.JAVAMELODY_VERSION != null
      && javamelodyVersion.compareTo(Parameters.JAVAMELODY_VERSION) > 0) {
    setNewJavamelodyVersion(javamelodyVersion);
  }
}

代码示例来源:origin: google/agera

@NonNull
private HttpResponse getHttpResponseResult(final @NonNull HttpRequest request,
  @NonNull final HttpURLConnection connection) throws IOException {
 connection.setConnectTimeout(request.connectTimeoutMs);
 connection.setReadTimeout(request.readTimeoutMs);
 connection.setInstanceFollowRedirects(request.followRedirects);
 connection.setUseCaches(request.useCaches);
 connection.setDoInput(true);
 connection.setRequestMethod(request.method);
 for (final Entry<String, String> headerField : request.header.entrySet()) {
  connection.addRequestProperty(headerField.getKey(), headerField.getValue());
 }
 final byte[] body = request.body;
 if (body.length > 0) {
  connection.setDoOutput(true);
  final OutputStream out = connection.getOutputStream();
  try {
   out.write(body);
  } finally {
   out.close();
  }
 }
 final String responseMessage = connection.getResponseMessage();
 return httpResponse(connection.getResponseCode(),
   responseMessage != null ? responseMessage : "",
   getHeader(connection), getByteArray(connection));
}

相关文章

HttpURLConnection类方法