java.net.URL#getHost ( )源码实例Demo

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

源代码1 项目: rome   文件: OpenSearchModuleParser.java
/**
 * Use xml:base attributes at feed and entry level to resolve relative links
 */
private static String resolveURI(final URL baseURI, final Parent parent, String url) {
    url = url.equals(".") || url.equals("./") ? "" : url;
    if (isRelativeURI(url) && parent != null && parent instanceof Element) {
        final Attribute baseAtt = ((Element) parent).getAttribute("base", Namespace.XML_NAMESPACE);
        String xmlBase = baseAtt == null ? "" : baseAtt.getValue();
        if (!isRelativeURI(xmlBase) && !xmlBase.endsWith("/")) {
            xmlBase = xmlBase.substring(0, xmlBase.lastIndexOf("/") + 1);
        }
        return resolveURI(baseURI, parent.getParent(), xmlBase + url);
    } else if (isRelativeURI(url) && parent == null) {
        return baseURI + url;
    } else if (baseURI != null && url.startsWith("/")) {
        String hostURI = baseURI.getProtocol() + "://" + baseURI.getHost();
        if (baseURI.getPort() != baseURI.getDefaultPort()) {
            hostURI = hostURI + ":" + baseURI.getPort();
        }
        return hostURI + url;
    }
    return url;
}
 
源代码2 项目: openjdk-8   文件: HttpsClient.java
/**
 *  Same as previous constructor except using a Proxy
 */
HttpsClient(SSLSocketFactory sf, URL url, Proxy proxy,
            int connectTimeout)
    throws IOException {
    this.proxy = proxy;
    setSSLSocketFactory(sf);
    this.proxyDisabled = true;

    this.host = url.getHost();
    this.url = url;
    port = url.getPort();
    if (port == -1) {
        port = getDefaultPort();
    }
    setConnectTimeout(connectTimeout);
    openServer();
}
 
源代码3 项目: openjdk-8   文件: HttpURLConnection.java
private String getHostAndPort(URL url) {
    String host = url.getHost();
    final String hostarg = host;
    try {
        // lookup hostname and use IP address if available
        host = AccessController.doPrivileged(
            new PrivilegedExceptionAction<String>() {
                public String run() throws IOException {
                        InetAddress addr = InetAddress.getByName(hostarg);
                        return addr.getHostAddress();
                }
            }
        );
    } catch (PrivilegedActionException e) {}
    int port = url.getPort();
    if (port == -1) {
        String scheme = url.getProtocol();
        if ("http".equals(scheme)) {
            return host + ":80";
        } else { // scheme must be https
            return host + ":443";
        }
    }
    return host + ":" + Integer.toString(port);
}
 
static String getLocation(HttpURLConnection connection, String path) throws MalformedURLException {
    if (connection == null || TextUtils.isEmpty(path)) {
        return null;
    }
    String location = connection.getHeaderField("Location");
    if (TextUtils.isEmpty(location)) {
        location = connection.getHeaderField("location");
    }
    if (TextUtils.isEmpty(location)) {
        return null;
    }
    if (!(location.startsWith("http://") || location
            .startsWith("https://"))) {
        //某些时候会省略host,只返回后面的path,所以需要补全url
        URL originUrl = new URL(path);
        location = originUrl.getProtocol() + "://"
                + originUrl.getHost() + location;
    }
    return location;
}
 
源代码5 项目: java-unified-sdk   文件: EngineSessionCookie.java
public void wrappCookie(boolean inResponse) {
  if (inResponse) {
    HttpServletResponse resp = responseHolder.get();
    HttpServletRequest req = requestHolder.get();
    if (resp != null) {
      AVUser u = AVUser.getCurrentUser();
      String host = null;
      try {
        URL requestURL = new URL(req.getRequestURL().toString());
        host = requestURL.getHost();
      } catch (Exception e) {
        LOGGER.w(e);
      }
      addCookie(req, resp, sign.encodeUser(u));
      addCookie(req, resp, sign.getCookieSign(u));
    }
  } else {
    responseHolder.set(null);
  }
}
 
源代码6 项目: TencentKona-8   文件: HttpURLConnection.java
private String getHostAndPort(URL url) {
    String host = url.getHost();
    final String hostarg = host;
    try {
        // lookup hostname and use IP address if available
        host = AccessController.doPrivileged(
            new PrivilegedExceptionAction<String>() {
                public String run() throws IOException {
                        InetAddress addr = InetAddress.getByName(hostarg);
                        return addr.getHostAddress();
                }
            }
        );
    } catch (PrivilegedActionException e) {}
    int port = url.getPort();
    if (port == -1) {
        String scheme = url.getProtocol();
        if ("http".equals(scheme)) {
            return host + ":80";
        } else { // scheme must be https
            return host + ":443";
        }
    }
    return host + ":" + Integer.toString(port);
}
 
源代码7 项目: Bytecoder   文件: XMLEntityManager.java
public static OutputStream createOutputStream(String uri) throws IOException {
    // URI was specified. Handle relative URIs.
    final String expanded = XMLEntityManager.expandSystemId(uri, null, true);
    final URL url = new URL(expanded != null ? expanded : uri);
    OutputStream out = null;
    String protocol = url.getProtocol();
    String host = url.getHost();
    // Use FileOutputStream if this URI is for a local file.
    if (protocol.equals("file")
            && (host == null || host.length() == 0 || host.equals("localhost"))) {
        File file = new File(getPathWithoutEscapes(url.getPath()));
        if (!file.exists()) {
            File parent = file.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
        }
        out = new FileOutputStream(file);
    }
    // Try to write to some other kind of URI. Some protocols
    // won't support this, though HTTP should work.
    else {
        URLConnection urlCon = url.openConnection();
        urlCon.setDoInput(false);
        urlCon.setDoOutput(true);
        urlCon.setUseCaches(false); // Enable tunneling.
        if (urlCon instanceof HttpURLConnection) {
            // The DOM L3 REC says if we are writing to an HTTP URI
            // it is to be done with an HTTP PUT.
            HttpURLConnection httpCon = (HttpURLConnection) urlCon;
            httpCon.setRequestMethod("PUT");
        }
        out = urlCon.getOutputStream();
    }
    return out;
}
 
源代码8 项目: jslt   文件: BuiltinFunctions.java
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  if (arguments[0].isNull())
    return NullNode.instance;

  String urlString = arguments[0].asText();

  try {
    URL aURL = new URL(arguments[0].asText());
    final ObjectNode objectNode = NodeUtils.mapper.createObjectNode();
    if (aURL.getHost() != null && !aURL.getHost().isEmpty())
      objectNode.put("host", aURL.getHost());
    if (aURL.getPort() != -1)
      objectNode.put("port", aURL.getPort());
    if (!aURL.getPath().isEmpty())
      objectNode.put("path", aURL.getPath());
    if (aURL.getProtocol() != null && !aURL.getProtocol().isEmpty())
      objectNode.put("scheme", aURL.getProtocol());
    if (aURL.getQuery() != null && !aURL.getQuery().isEmpty()) {
      objectNode.put("query", aURL.getQuery());
      final ObjectNode queryParamsNode = NodeUtils.mapper.createObjectNode();
      objectNode.set("parameters", queryParamsNode);
      final String[] pairs = aURL.getQuery().split("&");
      for (String pair : pairs) {
        final int idx = pair.indexOf("=");
        final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
        if (!queryParamsNode.has(key)) queryParamsNode.set(key, NodeUtils.mapper.createArrayNode());
        final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;
        final ArrayNode valuesNode = (ArrayNode) queryParamsNode.get(key);
        valuesNode.add(value);
      }
    }
    if(aURL.getRef() != null)
      objectNode.put("fragment", aURL.getRef());
    if(aURL.getUserInfo() != null && !aURL.getUserInfo().isEmpty())
      objectNode.put("userinfo", aURL.getUserInfo());
    return objectNode;
  } catch (MalformedURLException | UnsupportedEncodingException e) {
    throw new JsltException("Can't parse " + urlString, e);
  }
}
 
源代码9 项目: codeway_service   文件: OssClientUtil.java
/**
 * 上传文件
 * @param file 文件
 */
public String uploadFile(MultipartFile file) throws IOException {
	String fileName = file.getOriginalFilename();
	ossClient.putObject(BUCKET_NAME, fileName,file.getInputStream());
	URL url = ossClient.generatePresignedUrl(
			BUCKET_NAME,
			fileName,
			DateUtil.convertLdtToDate(DateUtil.getPlusMonths(99999)),
			HttpMethod.GET);
	return "https://"+url.getHost()+url.getPath();
}
 
源代码10 项目: tomee   文件: RESTService.java
private static String getFullContext(final String address, final String context) {
    if (context == null) {
        return address;
    }
    if (context.isEmpty() && address.contains("/")) {
        return address.substring(0, address.lastIndexOf("/"));
    }

    // context can get the app path too
    // so keep only web context without /
    String webCtx = context;
    while (webCtx.startsWith("/")) {
        webCtx = webCtx.substring(1);
    }

    // get root path ending with /
    try {
        final URL url = new URL(address);
        final int port = url.getPort();
        if (port > 0) {
            return url.getProtocol() + "://" + url.getHost() + ":" + port + "/" + webCtx;
        } else {
            return url.getProtocol() + "://" + url.getHost() + "/" + webCtx;
        }
    } catch (final MalformedURLException e) {
        throw new OpenEJBRestRuntimeException("bad url: " + address, e);
    }
}
 
public static String getDomainName(String url) throws MalformedURLException{
    if(!url.startsWith("http") && !url.startsWith("https")){
         url = "http://" + url;
    }        
    URL netUrl = new URL(url);
    String host = netUrl.getHost();
    if(host.startsWith("www")){
        host = host.substring("www".length()+1);
    }
    return host;
}
 
源代码12 项目: openapi-generator   文件: URLPathUtils.java
/**
 * Get the protocol and the host, example value <code>https://www.abcdef.xyz</code>
 *
 * @param url server url
 * @return protocolAndHost
 */
public static String getProtocolAndHost(URL url) {
    if (url == null) {
        return LOCAL_HOST;
    } else {
        String protocol = (url.getProtocol() == null) ? "http" : url.getProtocol();
        return protocol + "://" + url.getHost();
    }
}
 
源代码13 项目: HtmlUnit-Android   文件: CookieManager.java
/**
 * Helper that builds a CookieOrigin.
 * @param url the url to be used
 * @return the new CookieOrigin
 */
public CookieOrigin buildCookieOrigin(final URL url) {
    final URL normalizedUrl = replaceForCookieIfNecessary(url);

    return new CookieOrigin(
            normalizedUrl.getHost(),
            getPort(normalizedUrl),
            normalizedUrl.getPath(),
            "https".equals(normalizedUrl.getProtocol()));
}
 
源代码14 项目: Overchan-Android   文件: DobroModule.java
private String sanitizeUrl(String urlStr) {
    if (urlStr == null) return null;
    try {
        URL url = new URL(urlStr);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
        return uri.toURL().toString();
    } catch (Exception e) {
        Logger.e(TAG, "sanitize url", e);
        return urlStr;
    }
}
 
源代码15 项目: occurrence   文件: EsDatasetDeleterService.java
private RestHighLevelClient createEsClient() {
  HttpHost[] hosts = new HttpHost[config.esHosts.length];
  int i = 0;
  for (String host : config.esHosts) {
    try {
      URL url = new URL(host);
      hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
      i++;
    } catch (MalformedURLException e) {
      throw new IllegalArgumentException(e.getMessage(), e);
    }
  }

  SniffOnFailureListener sniffOnFailureListener =
    new SniffOnFailureListener();

  RestClientBuilder builder =
      RestClient.builder(hosts)
          .setRequestConfigCallback(
              requestConfigBuilder ->
                  requestConfigBuilder
                      .setConnectTimeout(config.esConnectTimeout)
                      .setSocketTimeout(config.esSocketTimeout))
          .setMaxRetryTimeoutMillis(config.esSocketTimeout)
          .setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS)
          .setFailureListener(sniffOnFailureListener);

  RestHighLevelClient highLevelClient = new RestHighLevelClient(builder);

  esSniffer =
    Sniffer.builder(highLevelClient.getLowLevelClient())
      .setSniffIntervalMillis(config.esSniffInterval)
      .setSniffAfterFailureDelayMillis(config.esSniffAfterFailureDelay)
      .build();
  sniffOnFailureListener.setSniffer(esSniffer);

  return highLevelClient;
}
 
源代码16 项目: vertx-stack   文件: ResolverImpl.java
private Proxy getHttpsProxy(String httpsProxy) {
  Proxy secureProxy = null;
  if (httpsProxy != null) {
    URL url = url(httpsProxy);
    Authentication authentication = extractAuth(url);
    secureProxy = new Proxy("https", url.getHost(), url.getPort(), authentication);
  }
  return secureProxy;
}
 
源代码17 项目: J2EEScan   文件: OracleEBSSSRF.java
@RunOnlyOnce
public List<IScanIssue> scan(IBurpExtenderCallbacks callbacks, IHttpRequestResponse baseRequestResponse, IScannerInsertionPoint insertionPoint) {

    IExtensionHelpers helpers = callbacks.getHelpers();
    stderr = new PrintWriter(callbacks.getStderr(), true);
    
    List<IScanIssue> issues = new ArrayList<>();

    IRequestInfo reqInfo = helpers.analyzeRequest(baseRequestResponse);

    URL url = reqInfo.getUrl();
    String host = url.getHost();
    int port = url.getPort();
    String protocol = url.getProtocol();
    Boolean isSSL = (protocol.equals("https"));

    String system = host.concat(Integer.toString(port));

    // Collaborator context
    IBurpCollaboratorClientContext collaboratorContext = callbacks.createBurpCollaboratorClientContext();
    String currentCollaboratorPayload = collaboratorContext.generatePayload(true);
    
    String Oracle_SSRF_Help = String.format("/OA_HTML/help?locale=en_AE&group=per:br_prod_HR:US&topic=http://%s:80/", currentCollaboratorPayload);

    // System not yet tested for this vulnerability
    if (!hs.contains(system)) {

        hs.add(system);
        
        try {
            URL urlToTest = new URL(protocol, url.getHost(), url.getPort(), Oracle_SSRF_Help);

            byte[] helpSSRFtest = helpers.buildHttpRequest(urlToTest);

            byte[] responseBytes = callbacks.makeHttpRequest(url.getHost(),
                    url.getPort(), isSSL, helpSSRFtest);

            // Poll Burp Collaborator for remote interaction
            List<IBurpCollaboratorInteraction> collaboratorInteractions = collaboratorContext.fetchCollaboratorInteractionsFor(currentCollaboratorPayload);

            if (!collaboratorInteractions.isEmpty()) {

                issues.add(new CustomScanIssue(
                        baseRequestResponse.getHttpService(),
                        urlToTest,
                        new CustomHttpRequestResponse(helpSSRFtest, responseBytes, baseRequestResponse.getHttpService()),
                        TITLE,
                        DESCRIPTION,
                        REMEDY,
                        Risk.High,
                        Confidence.Certain
                ));
            }

        } catch (MalformedURLException ex) {
            stderr.println("Malformed URL Exception " + ex);
        }

    } 
    
    return issues;
    
}
 
源代码18 项目: socialauth   文件: GenerateToken.java
private void getAccessToken() throws Exception {
	SocialAuthConfig config = SocialAuthConfig.getDefault();
	config.load();
	SocialAuthManager manager = new SocialAuthManager();
	manager.setSocialAuthConfig(config);

	URL aURL = new URL(successURL);
	host = aURL.getHost();
	port = aURL.getPort();
	port = port == -1 ? 80 : port;
	callbackPath = aURL.getPath();

	if (tokenFilePath == null) {
		tokenFilePath = System.getProperty("user.home");
	}
	String url = manager.getAuthenticationUrl(providerId, successURL);
	startServer();

	if (Desktop.isDesktopSupported()) {
		Desktop desktop = Desktop.getDesktop();
		if (desktop.isSupported(Action.BROWSE)) {
			try {
				desktop.browse(URI.create(url));
				// return;
			} catch (IOException e) {
				// handled below
			}
		}
	}

	lock.lock();
	try {
		while (paramsMap == null && error == null) {
			gotAuthorizationResponse.awaitUninterruptibly();
		}
		if (error != null) {
			throw new IOException("User authorization failed (" + error
					+ ")");
		}
	} finally {
		lock.unlock();
	}
	stop();

	AccessGrant accessGrant = manager.createAccessGrant(providerId,
			paramsMap, successURL);

	Exporter.exportAccessGrant(accessGrant, tokenFilePath);

	LOG.info("Access Grant Object saved in a file  :: " + tokenFilePath
			+ File.separatorChar + accessGrant.getProviderId()
			+ "_accessGrant_file.txt");
}
 
源代码19 项目: gama   文件: FileUtils.java
public static String constructAbsoluteTempFilePath(final IScope scope, final URL url) {
	return CACHE.getAbsolutePath() + SEPARATOR + url.getHost() + URL_SEPARATOR_REPLACEMENT
			+ url.getPath().replace(SEPARATOR, URL_SEPARATOR_REPLACEMENT);

}
 
源代码20 项目: knopflerfish.org   文件: BundleURLStreamHandler.java
/**
 * Compares the host components of two URLs.
 * @param u1 the URL of the first host to compare
 * @param u2 the URL of the second host to compare
 * @return    <tt>true</tt> if and only if they
 * are equal, <tt>false</tt> otherwise.
 */
@Override
protected boolean hostsEqual(URL u1, URL u2) {
  final String s1 = u1.getHost();
  final String s2 = u2.getHost();
  return (s1 == s2) || (s1 != null && s1.equals(s2));
}