java.net.URL.getPort()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(332)

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

URL.getPort介绍

[英]Returns the port number of this URL or -1 if this URL has no explicit port.

If this URL has no explicit port, connections opened using this URL will use its #getDefaultPort().
[中]返回此URL的端口号,如果此URL没有显式端口,则返回-1。
如果此URL没有显式端口,则使用此URL打开的连接将使用其#getDefaultPort()。

代码示例

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

public DefaultEndpoint(String serviceUrl) {
  this.serviceUrl = serviceUrl;
  try {
    URL url = new URL(serviceUrl);
    this.networkAddress = url.getHost();
    this.port = url.getPort();
    this.isSecure = "https".equals(url.getProtocol());
    this.relativeUri = url.getPath();
  } catch (Exception e) {
    throw new IllegalArgumentException("Malformed serviceUrl: " + serviceUrl);
  }
}

代码示例来源:origin: apache/incubator-druid

private String getPoolKey(URL url)
 {
  return StringUtils.format(
    "%s://%s:%s", url.getProtocol(), url.getHost(), url.getPort() == -1 ? url.getDefaultPort() : url.getPort()
  );
 }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean matches(WebRequest request) {
  URL url = request.getUrl();
  String host = url.getHost();
  if (this.hosts.contains(host)) {
    return true;
  }
  int port = url.getPort();
  if (port == -1) {
    port = url.getDefaultPort();
  }
  String hostAndPort = host + ":" + port;
  return this.hosts.contains(hostAndPort);
}

代码示例来源:origin: apache/flink

/**
 * Method to validate if the given String represents a hostname:port.
 *
 * <p>Works also for ipv6.
 *
 * <p>See: http://stackoverflow.com/questions/2345063/java-common-way-to-validate-and-convert-hostport-to-inetsocketaddress
 *
 * @return URL object for accessing host and Port
 */
public static URL getCorrectHostnamePort(String hostPort) {
  try {
    URL u = new URL("http://" + hostPort);
    if (u.getHost() == null) {
      throw new IllegalArgumentException("The given host:port ('" + hostPort + "') doesn't contain a valid host");
    }
    if (u.getPort() == -1) {
      throw new IllegalArgumentException("The given host:port ('" + hostPort + "') doesn't contain a valid port");
    }
    return u;
  } catch (MalformedURLException e) {
    throw new IllegalArgumentException("The given host:port ('" + hostPort + "') is invalid", e);
  }
}

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

URL jenkins_url = new URL(rootURL);
int jenkins_port = jenkins_url.getPort();
if (jenkins_port == -1) {
  jenkins_port = 80;
if (jenkins_url.getPath().length() > 0) {
  props.put("path", jenkins_url.getPath());

代码示例来源:origin: scouter-project/scouter

ctx.apicall_name = url.getPath();
step.address = url.getHost() + ":" + url.getPort();

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

String urlStr = "http://abc.dev.domain.com/0007AC/ads/800x480 15sec h.264.mp4";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

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

public static String getClientIp(String serviceUrl) {
    try {
      URL url = new URL(serviceUrl);
      int port = url.getPort();
      if (port == -1) {
        port = url.getDefaultPort();
      }
      try (Socket socket = new Socket(url.getHost(), port)) {
        return socket.getLocalAddress().getHostAddress();
      }
    } catch (Exception e) {
      return SystemUtil.getFirstLocalNonLoopbackIpAddress();
    }
  }
}

代码示例来源:origin: square/okhttp

@Override public Permission getPermission() throws IOException {
 URL url = getURL();
 String hostname = url.getHost();
 int hostPort = url.getPort() != -1
   ? url.getPort()
   : HttpUrl.defaultPort(url.getProtocol());
 if (usingProxy()) {
  InetSocketAddress proxyAddress = (InetSocketAddress) client.proxy().address();
  hostname = proxyAddress.getHostName();
  hostPort = proxyAddress.getPort();
 }
 return new SocketPermission(hostname + ":" + hostPort, "connect, resolve");
}

代码示例来源:origin: scouter-project/scouter

ctx.apicall_name = url.getPath();
step.address = url.getHost() + ":" + url.getPort();

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

/**
 * Constructs an object importer.
 *
 * <p>Remote objects are imported from the web server that the given
 * applet has been loaded from.
 *
 * @param applet    the applet loaded from the <code>Webserver</code>.
 */
public ObjectImporter(Applet applet) {
  URL codebase = applet.getCodeBase();
  orgServername = servername = codebase.getHost();
  orgPort = port = codebase.getPort();
}

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

private String getRootUrl(String string) {
  try {
    final URL url = new URL(string);
    return new URL(url.getProtocol(), url.getHost(), url.getPort(), "").toString();
  } catch (MalformedURLException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: googleapis/google-cloud-java

public String getEndpoint() {
  URL url;
  try {
   url = new URL(getHost());
  } catch (MalformedURLException e) {
   throw new IllegalArgumentException("Invalid host: " + getHost(), e);
  }
  return String.format(
    "%s:%s", url.getHost(), url.getPort() < 0 ? url.getDefaultPort() : url.getPort());
 }
}

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

private HttpResponse execute(HttpUriRequest hr, DefaultHttpClient client, HttpContext context) throws ClientProtocolException, IOException{
  
  HttpResponse response = null;
  if(hr.getURI().getAuthority().contains("_")) {
    URL urlObj = hr.getURI().toURL();
    HttpHost host;
    if(urlObj.getPort() == -1) {
      host = new HttpHost(urlObj.getHost(), 80, urlObj.getProtocol());
    } else {
      host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
    }
    response = client.execute(host, hr, context);
  } else {
    response = client.execute(hr, context);
  }
  
  
  return response;
}

代码示例来源:origin: apache/flink

@Override
protected void initializeConnections() {
  URL contactUrl = NetUtils.getCorrectHostnamePort(seedBrokerAddresses[currentContactSeedBrokerIndex]);
  this.consumer = new SimpleConsumer(contactUrl.getHost(), contactUrl.getPort(), soTimeout, bufferSize, dummyClientId);
}

代码示例来源:origin: rest-assured/rest-assured

private CookieOrigin cookieOriginFromUri(String uri) {

    try {
      URL parsedUrl = new URL(uri);
      int port = parsedUrl.getPort() != -1 ? parsedUrl.getPort() : 80;
      return new CookieOrigin(
          parsedUrl.getHost(), port, parsedUrl.getPath(), "https".equals(parsedUrl.getProtocol()));
    } catch (MalformedURLException e) {
      throw new IllegalArgumentException(e);
    }
  }
}

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

URL url = new URL("http://" + host);
if (url.getHost() != null) {
  host = url.getHost();
  urlPort = url.getPort();

代码示例来源:origin: apache/incubator-druid

private String getHost(URL url)
{
 int port = url.getPort();
 if (port == -1) {
  final String protocol = url.getProtocol();
  if ("http".equalsIgnoreCase(protocol)) {
   port = 80;
  } else if ("https".equalsIgnoreCase(protocol)) {
   port = 443;
  } else {
   throw new IAE("Cannot figure out default port for protocol[%s], please set Host header.", protocol);
  }
 }
 return StringUtils.format("%s:%s", url.getHost(), port);
}

代码示例来源:origin: apache/flink

/**
 * Re-establish broker connection using the next available seed broker address.
 */
private void useNextAddressAsNewContactSeedBroker() {
  if (++currentContactSeedBrokerIndex == seedBrokerAddresses.length) {
    currentContactSeedBrokerIndex = 0;
  }
  URL newContactUrl = NetUtils.getCorrectHostnamePort(seedBrokerAddresses[currentContactSeedBrokerIndex]);
  this.consumer = new SimpleConsumer(newContactUrl.getHost(), newContactUrl.getPort(), soTimeout, bufferSize, dummyClientId);
}

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

/**
 * Appends a trailing '/' to a {@link URL} object. Does not append a slash if one is already present.
 *
 * @param originalURL The URL to append a slash to
 * @return a new URL object that ends in a slash
 */
public static URL appendTrailingSlash(URL originalURL) {
  try {
    return originalURL.getPath().endsWith("/") ? originalURL :
        new URL(originalURL.getProtocol(),
            originalURL.getHost(),
            originalURL.getPort(),
            originalURL.getFile() + '/');
  } catch (MalformedURLException ignored) { // shouldn't happen
    throw new IllegalArgumentException("Invalid resource URL: " + originalURL);
  }
}

相关文章