java.net.URLConnection.setReadTimeout()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(566)

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

URLConnection.setReadTimeout介绍

[英]Sets the maximum time to wait for an input stream read to complete before giving up. Reading will fail with a SocketTimeoutException if the timeout elapses before data becomes available. The default value of 0 disables read timeouts; read attempts will block indefinitely.
[中]设置放弃前等待输入流读取完成的最长时间。如果在数据可用之前超时,读取将失败,并出现SocketTimeoutException。默认值0将禁用读取超时;读取尝试将无限期阻止。

代码示例

代码示例来源:origin: stackoverflow.com

  1. URL url = new URL(urlPath);
  2. URLConnection con = url.openConnection();
  3. con.setConnectTimeout(connectTimeout);
  4. con.setReadTimeout(readTimeout);
  5. InputStream in = con.getInputStream();

代码示例来源:origin: lingochamp/FileDownloader

  1. public FileDownloadUrlConnection(URL url, Configuration configuration) throws IOException {
  2. if (configuration != null && configuration.proxy != null) {
  3. mConnection = url.openConnection(configuration.proxy);
  4. } else {
  5. mConnection = url.openConnection();
  6. }
  7. if (configuration != null) {
  8. if (configuration.readTimeout != null) {
  9. mConnection.setReadTimeout(configuration.readTimeout);
  10. }
  11. if (configuration.connectTimeout != null) {
  12. mConnection.setConnectTimeout(configuration.connectTimeout);
  13. }
  14. }
  15. }

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

  1. public boolean check(URL url) {
  2. try {
  3. URLConnection connection = url.openConnection();
  4. connection.setReadTimeout(networkTimeout);
  5. InputStream is = connection.getInputStream();
  6. is.close();
  7. return true;
  8. } catch (Exception e) {
  9. ActiveMQUtilLogger.LOGGER.failedToCheckURL(e, url == null ? " " : url.toString());
  10. return false;
  11. }
  12. }

代码示例来源:origin: stackoverflow.com

  1. try
  2. final URL aUrl = new URL(url);
  3. final URLConnection conn = aUrl.openConnection();
  4. conn.setReadTimeout(15 * 1000); // timeout for reading the google maps data: 15 secs
  5. conn.connect();

代码示例来源:origin: cSploit/android

  1. protected void writeCall(String methodName, Object[] args) throws IOException {
  2. huc = u.openConnection();
  3. huc.setDoOutput(true);
  4. huc.setDoInput(true);
  5. huc.setUseCaches(false);
  6. huc.setRequestProperty("Content-Type", "binary/message-pack");
  7. huc.setReadTimeout(0);
  8. OutputStream os = huc.getOutputStream();
  9. Packer pk = msgpack.createPacker(os);
  10. pk.writeArrayBegin(args.length+1);
  11. pk.write(methodName);
  12. for (Object arg : args)
  13. pk.write(arg);
  14. pk.writeArrayEnd();
  15. pk.close();
  16. os.close();
  17. }

代码示例来源:origin: osmandapp/Osmand

  1. if (request.referer != null)
  2. connection.setRequestProperty("Referer", request.referer); //$NON-NLS-1$
  3. connection.setConnectTimeout(CONNECTION_TIMEOUT);
  4. connection.setReadTimeout(CONNECTION_TIMEOUT);
  5. BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
  6. request.saveTile(inputStream);
  7. if (log.isDebugEnabled()) {

代码示例来源:origin: jenkinsci/jenkins

  1. con.setReadTimeout(PLUGIN_DOWNLOAD_READ_TIMEOUT);
  2. sha256 != null ? new DigestOutputStream(
  3. sha512 != null ? new DigestOutputStream(_out, sha512) : _out, sha256) : _out, sha1) : _out;
  4. InputStream in = con.getInputStream();
  5. CountingInputStream cin = new CountingInputStream(in)) {
  6. while ((len = cin.read(buf)) >= 0) {

代码示例来源:origin: lingochamp/FileDownloader

  1. @Test
  2. public void construct_noConfiguration_noAssigned() throws IOException {
  3. FileDownloadUrlConnection.Creator creator = new FileDownloadUrlConnection.Creator();
  4. creator.create("http://blog.dreamtobe.cn");
  5. verify(mConnection, times(0)).setConnectTimeout(anyInt());
  6. verify(mConnection, times(0)).setReadTimeout(anyInt());
  7. }

代码示例来源:origin: commons-io/commons-io

  1. /**
  2. * Copies bytes from the URL <code>source</code> to a file
  3. * <code>destination</code>. The directories up to <code>destination</code>
  4. * will be created if they don't already exist. <code>destination</code>
  5. * will be overwritten if it already exists.
  6. *
  7. * @param source the <code>URL</code> to copy bytes from, must not be {@code null}
  8. * @param destination the non-directory <code>File</code> to write bytes to
  9. * (possibly overwriting), must not be {@code null}
  10. * @param connectionTimeout the number of milliseconds until this method
  11. * will timeout if no connection could be established to the <code>source</code>
  12. * @param readTimeout the number of milliseconds until this method will
  13. * timeout if no data could be read from the <code>source</code>
  14. * @throws IOException if <code>source</code> URL cannot be opened
  15. * @throws IOException if <code>destination</code> is a directory
  16. * @throws IOException if <code>destination</code> cannot be written
  17. * @throws IOException if <code>destination</code> needs creating but can't be
  18. * @throws IOException if an IO error occurs during copying
  19. * @since 2.0
  20. */
  21. public static void copyURLToFile(final URL source, final File destination,
  22. final int connectionTimeout, final int readTimeout) throws IOException {
  23. final URLConnection connection = source.openConnection();
  24. connection.setConnectTimeout(connectionTimeout);
  25. connection.setReadTimeout(readTimeout);
  26. copyInputStreamToFile(connection.getInputStream(), destination);
  27. }

代码示例来源:origin: lingochamp/okdownload

  1. void configUrlConnection() throws IOException {
  2. Util.d(TAG, "config connection for " + url);
  3. if (configuration != null && configuration.proxy != null) {
  4. connection = url.openConnection(configuration.proxy);
  5. } else {
  6. connection = url.openConnection();
  7. }
  8. if (configuration != null) {
  9. if (configuration.readTimeout != null) {
  10. connection.setReadTimeout(configuration.readTimeout);
  11. }
  12. if (configuration.connectTimeout != null) {
  13. connection.setConnectTimeout(configuration.connectTimeout);
  14. }
  15. }
  16. }

代码示例来源:origin: stanfordnlp/CoreNLP

  1. /**
  2. * Returns all the text at the given URL.
  3. */
  4. public static String slurpURL(URL u, String encoding) throws IOException {
  5. String lineSeparator = System.lineSeparator();
  6. URLConnection uc = u.openConnection();
  7. uc.setReadTimeout(30000);
  8. InputStream is;
  9. try {
  10. is = uc.getInputStream();
  11. } catch (SocketTimeoutException e) {
  12. logger.error("Socket time out; returning empty string.");
  13. logger.err(throwableToStackTrace(e));
  14. return "";
  15. }
  16. try (BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding))) {
  17. StringBuilder buff = new StringBuilder(SLURP_BUFFER_SIZE); // make biggish
  18. for (String temp; (temp = br.readLine()) != null; ) {
  19. buff.append(temp);
  20. buff.append(lineSeparator);
  21. }
  22. return buff.toString();
  23. }
  24. }

代码示例来源:origin: stackoverflow.com

  1. URLConnection conn = url.openConnection();
  2. conn.setConnectTimeout(timeoutMs);
  3. conn.setReadTimeout(timeoutMs);
  4. in = conn.getInputStream();

代码示例来源:origin: TEAMMATES/teammates

  1. @SuppressWarnings("PMD.AssignmentInOperand") // necessary for reading stream response
  2. private static String readResponse(URLConnection conn) throws IOException {
  3. conn.setReadTimeout(10000);
  4. StringBuilder sb = new StringBuilder();
  5. try (InputStreamReader isr = new InputStreamReader(conn.getInputStream(), Const.SystemParams.ENCODING);
  6. BufferedReader rd = new BufferedReader(isr)) {
  7. String line;
  8. while ((line = rd.readLine()) != null) {
  9. sb.append(line);
  10. }
  11. }
  12. return sb.toString();
  13. }

代码示例来源:origin: lingochamp/okdownload

  1. @Test
  2. public void construct_noConfiguration_noAssigned() throws IOException {
  3. DownloadUrlConnection.Factory factory = new DownloadUrlConnection.Factory();
  4. factory.create("https://jacksgong.com");
  5. verify(urlConnection, never()).setConnectTimeout(anyInt());
  6. verify(urlConnection, never()).setReadTimeout(anyInt());
  7. }

代码示例来源:origin: Netflix/servo

  1. @Override
  2. public InputStream fetchStatus() throws IOException {
  3. final URLConnection con = url.openConnection();
  4. con.setConnectTimeout(1000);
  5. con.setReadTimeout(2000);
  6. return con.getInputStream();
  7. }
  8. }

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

  1. /**
  2. * Ouvre la connection http.
  3. * @return Object
  4. * @throws IOException Exception de communication
  5. */
  6. private URLConnection openConnection() throws IOException {
  7. final URLConnection connection = url.openConnection();
  8. connection.setUseCaches(false);
  9. if (CONNECTION_TIMEOUT > 0) {
  10. connection.setConnectTimeout(CONNECTION_TIMEOUT);
  11. }
  12. if (READ_TIMEOUT > 0) {
  13. connection.setReadTimeout(READ_TIMEOUT);
  14. }
  15. // grâce à cette propriété, l'application retournera un flux compressé si la taille
  16. // dépasse x Ko
  17. connection.setRequestProperty("Accept-Encoding", "gzip");
  18. if (headers != null) {
  19. for (final Map.Entry<String, String> entry : headers.entrySet()) {
  20. connection.setRequestProperty(entry.getKey(), entry.getValue());
  21. }
  22. }
  23. if (url.getUserInfo() != null) {
  24. final String authorization = Base64Coder.encodeString(url.getUserInfo());
  25. connection.setRequestProperty("Authorization", "Basic " + authorization);
  26. }
  27. return connection;
  28. }

代码示例来源:origin: stackoverflow.com

  1. /**
  2. * Pings a HTTP URL. This effectively sends a HEAD request and returns <code>true</code> if the response code is in
  3. * the 200-399 range.
  4. * @param url The HTTP URL to be pinged.
  5. * @param timeout The timeout in millis for both the connection timeout and the response read timeout. Note that
  6. * the total timeout is effectively two times the given timeout.
  7. * @return <code>true</code> if the given HTTP URL has returned response code 200-399 on a HEAD request within the
  8. * given timeout, otherwise <code>false</code>.
  9. */
  10. public static boolean pingURL(String url, int timeout) {
  11. url = url.replaceFirst("^https", "http"); // Otherwise an exception may be thrown on invalid SSL certificates.
  12. try {
  13. HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
  14. connection.setConnectTimeout(timeout);
  15. connection.setReadTimeout(timeout);
  16. connection.setRequestMethod("HEAD");
  17. int responseCode = connection.getResponseCode();
  18. return (200 <= responseCode && responseCode <= 399);
  19. } catch (IOException exception) {
  20. return false;
  21. }
  22. }

代码示例来源:origin: lingochamp/FileDownloader

  1. @Test
  2. public void construct_validConfiguration_Assigned() throws IOException {
  3. FileDownloadUrlConnection.Creator creator = new FileDownloadUrlConnection.Creator(
  4. new FileDownloadUrlConnection.Configuration()
  5. .proxy(mProxy)
  6. .connectTimeout(1001)
  7. .readTimeout(1002)
  8. );
  9. creator.create(mURL);
  10. verify(mURL).openConnection(mProxy);
  11. verify(mConnection).setConnectTimeout(1001);
  12. verify(mConnection).setReadTimeout(1002);
  13. }

代码示例来源:origin: stackoverflow.com

  1. URL url = new URL("http://yoururl.com");
  2. HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
  3. conn.setReadTimeout(10000);
  4. conn.setConnectTimeout(15000);
  5. conn.setRequestMethod("POST");
  6. conn.setDoInput(true);
  7. conn.setDoOutput(true);
  8. Uri.Builder builder = new Uri.Builder()
  9. .appendQueryParameter("firstParam", paramValue1)
  10. .appendQueryParameter("secondParam", paramValue2)
  11. .appendQueryParameter("thirdParam", paramValue3);
  12. String query = builder.build().getEncodedQuery();
  13. OutputStream os = conn.getOutputStream();
  14. BufferedWriter writer = new BufferedWriter(
  15. new OutputStreamWriter(os, "UTF-8"));
  16. writer.write(query);
  17. writer.flush();
  18. writer.close();
  19. os.close();
  20. conn.connect();

代码示例来源:origin: lingochamp/okdownload

  1. @Test
  2. public void construct_validConfiguration_Assigned() throws IOException {
  3. DownloadUrlConnection.Factory factory = new DownloadUrlConnection.Factory(
  4. new DownloadUrlConnection.Configuration()
  5. .proxy(proxy)
  6. .connectTimeout(123)
  7. .readTimeout(456)
  8. );
  9. factory.create(url);
  10. verify(url).openConnection(proxy);
  11. verify(urlConnection).setConnectTimeout(123);
  12. verify(urlConnection).setReadTimeout(456);
  13. }

相关文章

URLConnection类方法