java.net.URLConnection#setConnectTimeout ( )源码实例Demo

下面列出了java.net.URLConnection#setConnectTimeout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: dragonite-java   文件: UpdateChecker.java
public String getVersionString(final String productName) {
    try {
        final URL url = new URL(URLString);
        final URLConnection conn = url.openConnection();
        conn.setConnectTimeout(TIMEOUT);
        conn.setReadTimeout(TIMEOUT);
        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("-")) {
                    final String[] kv = line.split("-");
                    if (kv[0].equalsIgnoreCase(productName))
                        return kv[1];
                }
            }
        }
        return null;
    } catch (final IOException e) {
        return null;
    }
}
 
源代码2 项目: open-rmbt   文件: JSONParser.java
public static String readUrl(final URL url) throws IOException 
{
    final URLConnection urlConnection = url.openConnection();
    try {
        urlConnection.setConnectTimeout(CONNECT_TIMEOUT);
        urlConnection.setReadTimeout(READ_TIMEOUT);
        
        final StringBuilder stringBuilder = new StringBuilder();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        int read;
        final char[] chars = new char[1024];
        while ((read = reader.read(chars)) != -1)
            stringBuilder.append(chars, 0, read);
        return stringBuilder.toString();
    } finally {
        if (urlConnection instanceof HttpURLConnection)
            ((HttpURLConnection)urlConnection).disconnect();
    }
}
 
源代码3 项目: nyzoVerifier   文件: NetworkUtil.java
public static String stringForUrl(String urlString, int timeoutMilliseconds) {

        StringBuilder result = new StringBuilder();
        try {
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();
            connection.setConnectTimeout(timeoutMilliseconds);

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

            reader.close();
        } catch (Exception ignored) { }

        return result.toString();
    }
 
源代码4 项目: openshift-ping   文件: InsecureStreamProvider.java
public URLConnection openConnection(String url, Map<String, String> headers, int connectTimeout, int readTimeout)
        throws IOException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format(
                "%s opening connection: url [%s], headers [%s], connectTimeout [%s], readTimeout [%s]", getClass()
                        .getSimpleName(), url, headers, connectTimeout, readTimeout));
    }
    URLConnection connection = new URL(url).openConnection(Proxy.NO_PROXY);
    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            connection.addRequestProperty(entry.getKey(), entry.getValue());
        }
    }
    if (connectTimeout < 0 || readTimeout < 0) {
        throw new IllegalArgumentException(String.format(
                "Neither connectTimeout [%s] nor readTimeout [%s] can be less than 0 for URLConnection.",
                connectTimeout, readTimeout));
    }
    connection.setConnectTimeout(connectTimeout);
    connection.setReadTimeout(readTimeout);
    return connection;
}
 
源代码5 项目: SikuliX1   文件: ExtensionManagerFrame.java
private static String html2txt(String urlstring) throws IOException {
  URL url = new URL(urlstring);
  URLConnection yc = url.openConnection();
  yc.setConnectTimeout(5000);
  BufferedReader in = new BufferedReader(
          new InputStreamReader(
          yc.getInputStream()));
  String txt = "";
  String inputLine;

  while ((inputLine = in.readLine()) != null) {
    txt = txt + inputLine;
  }
  in.close();

  return txt;

}
 
源代码6 项目: jwks-rsa-java   文件: UrlJwkProvider.java
private Map<String, Object> getJwks() throws SigningKeyNotFoundException {
    try {
        final URLConnection c = this.url.openConnection();
        if (connectTimeout != null) {
            c.setConnectTimeout(connectTimeout);
        }
        if (readTimeout != null) {
            c.setReadTimeout(readTimeout);
        }
        c.setRequestProperty("Accept", "application/json");
        
        try (InputStream inputStream = c.getInputStream()) {
            return reader.readValue(inputStream);
        }
    } catch (IOException e) {
        throw new NetworkException("Cannot obtain jwks from url " + url.toString(), e);
    }
}
 
源代码7 项目: lams   文件: UpdateChecker.java
private Properties getUpdateProperties(URL updateUrl) throws IOException {
  URLConnection connection = updateUrl.openConnection();
  connection.setConnectTimeout(3 * 1000);
  InputStream in = connection.getInputStream();
  try {
    Properties props = new Properties();
    props.load(connection.getInputStream());
    return props;
  } finally {
    if (in != null) {
      in.close();
    }
  }
}
 
源代码8 项目: eclipse-cs   文件: RemoteConfigurationType.java
@Override
protected byte[] getBytesFromURLConnection(URLConnection connection) throws IOException {

  byte[] configurationFileData = null;

  // set timeouts - bug 2941010
  connection.setConnectTimeout(10000);
  connection.setReadTimeout(10000);

  if (connection instanceof HttpURLConnection) {

    if (!sFailedWith401URLs.contains(connection.getURL().toString())) {

      HttpURLConnection httpConn = (HttpURLConnection) connection;
      httpConn.setInstanceFollowRedirects(true);
      httpConn.connect();
      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
        try {
          RemoteConfigAuthenticator.removeCachedAuthInfo(connection.getURL());
        } catch (CheckstylePluginException e) {
          CheckstyleLog.log(e);
        }

        // add to 401ed URLs
        sFailedWith401URLs.add(connection.getURL().toString());
        throw new IOException(Messages.RemoteConfigurationType_msgUnAuthorized);
      }
    } else {
      // don't retry since we just get another 401
      throw new IOException(Messages.RemoteConfigurationType_msgUnAuthorized);
    }
  }

  try (InputStream in = connection.getInputStream()) {
    configurationFileData = ByteStreams.toByteArray(in);
  }

  return configurationFileData;
}
 
源代码9 项目: MaxKey   文件: NetUtil.java
/**
 * Open an input stream to a GET(-like) operation on an URL.
 * 
 * @param url The URL
 * @return Input stream to the URL connection
 * @throws IOException If an I/O error occurs
 */
public static InputStream openGetStream(URL url)
    throws IOException
{
	URLConnection conn = url.openConnection();

	conn.setConnectTimeout(CONNECT_TIMEOUT);
	conn.setReadTimeout(READ_TIMEOUT);

	// TODO: User-Agent?

	return conn.getInputStream();
}
 
源代码10 项目: ChestCommands   文件: SimpleUpdater.java
private Object readJson(String url) throws MalformedURLException, IOException {
	URLConnection conn = new URL(url).openConnection();
	conn.setConnectTimeout(5000);
	conn.setReadTimeout(8000);
	conn.addRequestProperty("User-Agent", "Updater (by filoghost)");
	conn.setDoOutput(true);

	return JSONValue.parse(new BufferedReader(new InputStreamReader(conn.getInputStream())));
}
 
源代码11 项目: HubTurbo   文件: FileDownloader.java
private URLConnection setupConnection(URI source) throws IOException {
    URLConnection connection = source.toURL().openConnection();
    connection.setConnectTimeout(CONNECTION_TIMEOUT);
    connection.setReadTimeout(READ_CONNECTION_TIMEOUT);

    return connection;
}
 
源代码12 项目: charging_pile_cloud   文件: HttpClientUtil.java
/**
 * 封装HTTP GET方法
 *
 * @param
 * @return
 * @throws Exception 
 */
public static String get(String url) throws Exception {
    URL u = new URL(url);  
    if("https".equalsIgnoreCase(u.getProtocol())){  
        SSLUtil.ignoreSsl();  
    }  
    URLConnection conn = u.openConnection();  
    conn.setConnectTimeout(3000);  
    conn.setReadTimeout(3000);  
    return IOUtils.toString(conn.getInputStream()); 
}
 
源代码13 项目: eagle   文件: InputStreamUtils.java
private static InputStream openGZIPInputStream(URL url, String auth, int timeout) throws IOException {
    final URLConnection connection = url.openConnection();
    connection.setConnectTimeout(CONNECTION_TIMEOUT);
    connection.setReadTimeout(timeout);
    connection.addRequestProperty(GZIP_HTTP_HEADER, GZIP_COMPRESSION);
    if (null != auth) {
        connection.setRequestProperty("Authorization", auth);
    }
    return new GZIPInputStream(connection.getInputStream());
}
 
源代码14 项目: sakai   文件: GetArchivesJob.java
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {

    String source = serverConfigurationService.getString("archives.import.source", null);
    if (source == null) {
        return;
    }

    log.info("Attempting to import archives listed in: "+ source);
    try {

        URL url = new URL(source);
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("User-Agent", "Sakai Content Importer");
        connection.setConnectTimeout(30000);
        connection.setReadTimeout(30000);
        // Now make the connection.
        connection.connect();
        try (InputStream inputStream = connection.getInputStream()) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                if (!line.isEmpty() && !line.startsWith("#")) {
                    String file = downloadArchive(line);
                    if (file != null) {
                        String siteId = extractSiteId(line);
                        scheduleImport(file, siteId);
                    }
                }
            }
        }

    } catch (IOException ioe) {
        log.warn("Problem with "+ source + " "+ ioe.getMessage());
    }
}
 
源代码15 项目: aptoide-client   文件: IntentReceiver.java
private void downloadMyappFile(String myappUri) throws Exception {
    try {
        URL url = new URL(myappUri);
        URLConnection connection;
        if (!myappUri.startsWith("file://")) {
            connection = url.openConnection();
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
        } else {
            connection = url.openConnection();
        }

        BufferedInputStream getit = new BufferedInputStream(connection.getInputStream(), 1024);

        File file_teste = new File(TMP_MYAPP_FILE);
        if (file_teste.exists())
            file_teste.delete();

        FileOutputStream saveit = new FileOutputStream(TMP_MYAPP_FILE);
        BufferedOutputStream bout = new BufferedOutputStream(saveit, 1024);
        byte data[] = new byte[1024];

        int readed = getit.read(data, 0, 1024);
        while (readed != -1) {
            bout.write(data, 0, readed);
            readed = getit.read(data, 0, 1024);
        }


        bout.close();
        getit.close();
        saveit.close();
    } catch (Exception e) {
        Logger.printException(e);
    }
}
 
源代码16 项目: XRTB   文件: HttpPostGet.java
/**
 * Send an HTTP Post
 * @param targetURL
 *    String. The URL to transmit to.
 * @param data
 *            String. The payload String.
 * @param connTimeout
 * 			  int. The connection timeout in ms
 * @param readTimeout
 * 			  int. The read timeout in ms.
 * @return String. The contents of the POST return.
 * @throws Exception
 *             on network errors.
 */

public String sendPost(String targetURL, String data, int connTimeout, int readTimeout) throws Exception {
	URLConnection connection = new URL(targetURL).openConnection();
	connection.setRequestProperty("Content-Type", "application/json");
	connection.setDoInput(true);
	connection.setDoOutput(true);
	connection.setConnectTimeout(connTimeout);
	connection.setReadTimeout(readTimeout);
	OutputStream output = connection.getOutputStream();
	
	try {
		output.write(data.getBytes());
	} finally {
		try {
			output.close();
		} catch (IOException logOrIgnore) {
			logOrIgnore.printStackTrace();
		}
	}
	InputStream response = connection.getInputStream();
	
	http = (HttpURLConnection) connection;
	code = http.getResponseCode();
	
	String value = http.getHeaderField("Content-Encoding");
	
	if (value != null && value.equals("gzip")) {
		byte bytes [] = new byte[4096];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		GZIPInputStream gis = new GZIPInputStream(http.getInputStream());
		while(true) {
			int n = gis.read(bytes);
			if (n < 0) break;
			baos.write(bytes,0,n);
		}
		return new String(baos.toByteArray());
	}
	
		

	byte [] b = getContents(response);
	if (b.length == 0)
		return null;
	return new String(b, 0, b.length);
}
 
源代码17 项目: bidder   文件: HttpPostGet.java
/**
 * Send an HTTP Post
 * @param targetURL
 *    String. The URL to transmit to.
 * @param data
 *            byte[]. The payload bytes.
 * @param connTimeout
 * 			  int. The connection timeout in ms
 * @param readTimeout
 * 			  int. The read timeout in ms.
 * @return byte[]. The contents of the POST return.
 * @throws Exception
 *             on network errors.
 */
public byte [] sendPost(String targetURL,  byte [] data, int connTimeout, int readTimeout) throws Exception {
	URLConnection connection = new URL(targetURL).openConnection();
	connection.setRequestProperty("Content-Type", "application/json");
	connection.setDoInput(true);
	connection.setDoOutput(true);
	connection.setConnectTimeout(connTimeout);
	connection.setReadTimeout(readTimeout);
	OutputStream output = connection.getOutputStream();
	
	try {
		output.write(data);
	} finally {
		try {
			output.close();
		} catch (IOException logOrIgnore) {
			logOrIgnore.printStackTrace();
		}
	}
	InputStream response = connection.getInputStream();
	
	http = (HttpURLConnection) connection;
	code = http.getResponseCode();
	
	String value = http.getHeaderField("Content-Encoding");
	
	if (value != null && value.equals("gzip")) {
		byte bytes [] = new byte[4096];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		GZIPInputStream gis = new GZIPInputStream(http.getInputStream());
		while(true) {
			int n = gis.read(bytes);
			if (n < 0) break;
			baos.write(bytes,0,n);
		}
		return baos.toByteArray();
	}
	
		

	byte [] b = getContents(response);
	if (b.length == 0)
		return null;
	return b;
}
 
源代码18 项目: sofa-hessian   文件: HessianURLConnectionFactory.java
/**
 * Opens a new or recycled connection to the HTTP server.
 */
public HessianConnection open(URL url)
    throws IOException
{
    if (log.isLoggable(Level.FINER))
        log.finer(this + " open(" + url + ")");

    URLConnection conn = url.openConnection();

    // HttpURLConnection httpConn = (HttpURLConnection) conn;
    // httpConn.setRequestMethod("POST");
    // conn.setDoInput(true);

    long connectTimeout = _proxyFactory.getConnectTimeout();

    if (connectTimeout >= 0)
        conn.setConnectTimeout((int) connectTimeout);

    conn.setDoOutput(true);

    long readTimeout = _proxyFactory.getReadTimeout();

    if (readTimeout > 0) {
        try {
            conn.setReadTimeout((int) readTimeout);
        } catch (Throwable e) {
        }
    }

    /*
    // Used chunked mode when available, i.e. JDK 1.5.
    if (_proxyFactory.isChunkedPost() && conn instanceof HttpURLConnection) {
      try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;

        httpConn.setChunkedStreamingMode(8 * 1024);
      } catch (Throwable e) {
      }
    }
    */

    return new HessianURLConnection(url, conn);
}
 
源代码19 项目: BedwarsRel   文件: PluginUpdater.java
/**
 * Make a connection to the BukkitDev API and request the newest file's details.
 *
 * @return true if successful.
 */
private boolean read() {
  try {
    final URLConnection conn = this.url.openConnection();
    conn.setConnectTimeout(5000);

    if (this.apiKey != null) {
      conn.addRequestProperty("X-API-Key", this.apiKey);
    }
    conn.addRequestProperty("User-Agent", PluginUpdater.USER_AGENT);
    conn.setDoOutput(true);

    final BufferedReader reader =
        new BufferedReader(new InputStreamReader(conn.getInputStream()));
    final String response = reader.readLine();

    final JSONArray array = (JSONArray) JSONValue.parse(response);

    if (array.isEmpty()) {
      this.plugin.getLogger()
          .warning("The updater could not find any files for the project id " + this.id);
      this.result = UpdateResult.FAIL_BADID;
      return false;
    }

    JSONObject latestUpdate = (JSONObject) array.get(array.size() - 1);
    this.versionName = (String) latestUpdate.get(PluginUpdater.TITLE_VALUE);
    this.versionLink = (String) latestUpdate.get(PluginUpdater.LINK_VALUE);
    this.versionType = (String) latestUpdate.get(PluginUpdater.TYPE_VALUE);
    this.versionGameVersion = (String) latestUpdate.get(PluginUpdater.VERSION_VALUE);
    this.versionCustom = this.getCustomVersion();

    return true;
  } catch (final IOException e) {
    if (e.getMessage().contains("HTTP response code: 403")) {
      this.plugin.getLogger()
          .severe("dev.bukkit.org rejected the API key provided in plugins/updater/config.yml");
      this.plugin.getLogger()
          .severe("Please double-check your configuration to ensure it is correct.");
      this.result = UpdateResult.FAIL_APIKEY;
    } else {
      this.plugin.getLogger()
          .severe("The updater could not contact dev.bukkit.org for updating.");
      this.plugin.getLogger().severe(
          "If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime.");
      this.result = UpdateResult.FAIL_DBO;
    }
    this.plugin.getLogger().log(Level.SEVERE, null, e);
    return false;
  }
}
 
源代码20 项目: askyblock   文件: Updater.java
/**
 * Make a connection to the BukkitDev API and request the newest file's details.
 *
 * @return true if successful.
 */
private boolean read() {
    try {
        final URLConnection conn = this.url.openConnection();
        conn.setConnectTimeout(5000);

        if (this.apiKey != null) {
            conn.addRequestProperty("X-API-Key", this.apiKey);
        }
        conn.addRequestProperty("User-Agent", Updater.USER_AGENT);

        conn.setDoOutput(true);

        final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        final String response = reader.readLine();

        final JSONArray array = (JSONArray) JSONValue.parse(response);

        if (array.isEmpty()) {
            this.plugin.getLogger().warning("The updater could not find any files for the project id " + this.id);
            this.result = UpdateResult.FAIL_BADID;
            return false;
        }

        JSONObject latestUpdate = (JSONObject) array.get(array.size() - 1);
        this.versionName = (String) latestUpdate.get(Updater.TITLE_VALUE);
        this.versionLink = (String) latestUpdate.get(Updater.LINK_VALUE);
        this.versionType = (String) latestUpdate.get(Updater.TYPE_VALUE);
        this.versionGameVersion = (String) latestUpdate.get(Updater.VERSION_VALUE);

        return true;
    } catch (final IOException e) {
        if (e.getMessage().contains("HTTP response code: 403")) {
            this.plugin.getLogger().severe("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml");
            this.plugin.getLogger().severe("Please double-check your configuration to ensure it is correct.");
            this.result = UpdateResult.FAIL_APIKEY;
        } else {
            this.plugin.getLogger().severe("The updater could not contact dev.bukkit.org for updating.");
            this.plugin.getLogger().severe("If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime.");
            this.result = UpdateResult.FAIL_DBO;
        }
        this.plugin.getLogger().log(Level.SEVERE, null, e);
        return false;
    }
}