org.apache.http.conn.util.InetAddressUtils Java Examples
The following examples show how to use
org.apache.http.conn.util.InetAddressUtils.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java From AirFree-Client with GNU General Public License v3.0 | 6 votes |
public String getIPAddress() { String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement(); Enumeration<InetAddress> inet = nif.getInetAddresses(); while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip .getHostAddress())) { return ipaddress = ip.getHostAddress(); } } } } catch (SocketException e) { Log.e("IP & PORT", "获取本地ip地址失败"); e.printStackTrace(); } return ipaddress; }
Example #2
Source File: NetworkUtils.java From Mizuu with Apache License 2.0 | 6 votes |
/** * Get IP address from first non-localhost interface. * Taken from http://stackoverflow.com/a/13007325/762442. * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string */ public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim<0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
Example #3
Source File: RemoteShareActivity.java From CameraV with GNU General Public License v3.0 | 6 votes |
public static String[] getLocalIpAddresses(){ try { ArrayList<String> alAddresses = new ArrayList<String>(); for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()&& InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { alAddresses.add(inetAddress.getHostAddress()); } } } return alAddresses.toArray(new String[alAddresses.size()]); } catch (Exception ex) { Log.e("IP Address", ex.toString()); } return null; }
Example #4
Source File: WifiUtils.java From WifiChat with GNU General Public License v2.0 | 6 votes |
public static String getGPRSLocalIPAddress() { try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement(); Enumeration<InetAddress> enumIpAddr = nif.getInetAddresses(); while (enumIpAddr.hasMoreElements()) { InetAddress mInetAddress = enumIpAddr.nextElement(); if (!mInetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(mInetAddress.getHostAddress())) { return mInetAddress.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
Example #5
Source File: NetworkInfo.java From PADListener with GNU General Public License v2.0 | 6 votes |
public static String getIPAddress(boolean useIPv4) throws SocketException { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { if (sAddr.startsWith("fe80") || sAddr.startsWith("FE80")) // skipping link-local addresses continue; int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim<0 ? sAddr : sAddr.substring(0, delim); } } } } } return ""; }
Example #6
Source File: Utils.java From BeyondUPnP with Apache License 2.0 | 6 votes |
/** * Get IP address from first non-localhost interface * * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string */ public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
Example #7
Source File: WifiUtils.java From FlyWoo with Apache License 2.0 | 6 votes |
public static String getGPRSLocalIPAddress() { try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement(); Enumeration<InetAddress> enumIpAddr = nif.getInetAddresses(); while (enumIpAddr.hasMoreElements()) { InetAddress mInetAddress = enumIpAddr.nextElement(); if (!mInetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(mInetAddress.getHostAddress())) { return mInetAddress.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
Example #8
Source File: TalkActivity.java From AirFree-Client with GNU General Public License v3.0 | 6 votes |
public String getIPAddress() { String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement(); Enumeration<InetAddress> inet = nif.getInetAddresses(); while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip .getHostAddress())) { return ip.getHostAddress(); } } } } catch (SocketException e) { Log.e("IP & PORT", "获取本地ip地址失败"); e.printStackTrace(); } return ipaddress; }
Example #9
Source File: NetBasicInfo.java From AndroidHttpCapture with MIT License | 6 votes |
public String GetIp(Boolean isV4) { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr.hasMoreElements(); ) { InetAddress inetAddress = ipAddr.nextElement(); if(isV4) { // ipv4地址 if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { return inetAddress.getHostAddress(); } }else{ // ipv6地址 if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv6Address(inetAddress.getHostAddress())) { return inetAddress.getHostAddress(); } } } } } catch (Exception ex) { // } return ""; }
Example #10
Source File: NetAddressUtil.java From BigApp_Discuz_Android with Apache License 2.0 | 6 votes |
/** * 获取设备IP地址 * * @return 设备ip地址 */ public static String getLocalIpAddress(){ try{ for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) { return inetAddress.getHostAddress().toString(); } } } }catch (SocketException e) { // TODO: handle exception Log.v("Steel", "WifiPreference IpAddress---error-" + e.toString()); } return null; }
Example #11
Source File: AbstractVerifierFix.java From steady with Apache License 2.0 | 5 votes |
private String normaliseIPv6Address(final String hostname) { if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) { return hostname; } try { final InetAddress inetAddress = InetAddress.getByName(hostname); return inetAddress.getHostAddress(); } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above log.error("Unexpected error converting "+hostname, uhe); return hostname; } }
Example #12
Source File: LFXNetworkUtils.java From lifx-sdk-android with MIT License | 5 votes |
@SuppressLint("DefaultLocale") public static String getLocalHostAddress() { boolean useIPv4 = true; try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) { return sAddr; } } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
Example #13
Source File: ConnectionConfiguration.java From AndroidPNClient with Apache License 2.0 | 5 votes |
/** * Returns the host to use when establishing the connection. The host and port to use * might have been resolved by a DNS lookup as specified by the XMPP spec (and therefore * may not match the {@link #getServiceName service name}. * * @return the host to use when establishing the connection. */ public String getHost() { // 如果不是IP,则尝试将它以域名进行IP转换。 if(!InetAddressUtils.isIPv4Address(host) && !InetAddressUtils.isIPv6Address(host)) { try { InetAddress address = InetAddress.getByName(host); host = address.getHostAddress(); Log.d(LOG_TAG, "transform host name to host address:"+host); } catch (UnknownHostException e) { Log.e(LOG_TAG, e.getMessage(), e); } } return host; }
Example #14
Source File: J_AbstractVerifier_V.java From steady with Apache License 2.0 | 5 votes |
private String normaliseIPv6Address(final String hostname) { if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) { return hostname; } try { final InetAddress inetAddress = InetAddress.getByName(hostname); return inetAddress.getHostAddress(); } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above log.error("Unexpected error converting "+hostname, uhe); return hostname; } }
Example #15
Source File: WebTools.java From zrlog with Apache License 2.0 | 5 votes |
/** * 处理由于浏览器使用透明代理,或者是WebServer运行在诸如 nginx/apache 这类 HttpServer后面的情况,通过获取请求头真实IP地址 * * @param request * @return */ public static String getRealIp(HttpServletRequest request) { String ip = null; //bae env if (ZrLogUtil.isBae() && request.getHeader("clientip") != null) { ip = request.getHeader("clientip"); } if (ip == null || ip.length() == 0) { ip = request.getHeader("X-forwarded-for"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } if (InetAddressUtils.isIPv4Address(ip) || InetAddressUtils.isIPv6Address(ip)) { return ip; } throw new IllegalArgumentException(ip + " not ipAddress"); }
Example #16
Source File: TcpDiscoveryAlbIpFinder.java From ignite with Apache License 2.0 | 5 votes |
/** * Checks if the given id is a valid IP address * * @param id ip to be checked. */ private boolean isIPAddress(String id) { return InetAddressUtils.isIPv4Address(id) || InetAddressUtils.isIPv4MappedIPv64Address(id) || InetAddressUtils.isIPv6Address(id) || InetAddressUtils.isIPv6HexCompressedAddress(id) || InetAddressUtils.isIPv6StdAddress(id); }
Example #17
Source File: BridgeSettings.java From ha-bridge with Apache License 2.0 | 5 votes |
private String checkIpAddress(String ipAddress, boolean checkForLocalhost) { Enumeration<NetworkInterface> ifs = null; try { ifs = NetworkInterface.getNetworkInterfaces(); } catch(SocketException e) { log.error("checkIpAddress cannot get ip address of this host, Exiting with message: " + e.getMessage(), e); return null; } String addressString = null; InetAddress address = null; while (ifs.hasMoreElements() && addressString == null) { NetworkInterface xface = ifs.nextElement(); Enumeration<InetAddress> addrs = xface.getInetAddresses(); String name = xface.getName(); int IPsPerNic = 0; while (addrs.hasMoreElements() && IPsPerNic == 0) { address = addrs.nextElement(); if (InetAddressUtils.isIPv4Address(address.getHostAddress())) { log.debug(name + " ... has IPV4 addr " + address); if(checkForLocalhost && (!name.equalsIgnoreCase(Configuration.LOOP_BACK_INTERFACE) || !address.getHostAddress().equalsIgnoreCase(Configuration.LOOP_BACK_ADDRESS))) { IPsPerNic++; addressString = address.getHostAddress(); log.debug("checkIpAddress found " + addressString + " from interface " + name); } else if(ipAddress != null && ipAddress.equalsIgnoreCase(address.getHostAddress())){ addressString = ipAddress; IPsPerNic++; } } } } return addressString; }
Example #18
Source File: AbstractCommonHostnameVerifierFix.java From steady with Apache License 2.0 | 5 votes |
private String normaliseIPv6Address(final String hostname) { if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) { return hostname; } try { final InetAddress inetAddress = InetAddress.getByName(hostname); return inetAddress.getHostAddress(); } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above log.error("Unexpected error converting "+hostname, uhe); return hostname; } }
Example #19
Source File: HostHeaderHandler.java From nifi with Apache License 2.0 | 5 votes |
/** * Returns true if the provided address is an IPv6 address (or could be interpreted as one). This method is more * lenient than {@link InetAddressUtils#isIPv6Address(String)} because of different interpretations of IPv4-mapped * IPv6 addresses. * * See RFC 5952 Section 4 for more information on textual representation of the IPv6 addresses. * * @param address the address in text form * @return true if the address is or could be parsed as an IPv6 address */ static boolean isIPv6Address(String address) { // Note: InetAddressUtils#isIPv4MappedIPv64Address() fails on addresses that do not compress the leading 0:0:0... to :: // Expanded for debugging purposes boolean isNormalIPv6 = InetAddressUtils.isIPv6Address(address); // If the last two hextets are written in IPv4 form, treat it as an IPv6 address as well String everythingAfterLastColon = StringUtils.substringAfterLast(address, ":"); boolean isIPv4 = InetAddressUtils.isIPv4Address(everythingAfterLastColon); boolean isIPv4Mapped = InetAddressUtils.isIPv4MappedIPv64Address(everythingAfterLastColon); boolean isCompressable = address.contains("0:0") && !address.contains("::"); return isNormalIPv6 || isIPv4; }
Example #20
Source File: J_AbstractVerifier_F.java From steady with Apache License 2.0 | 5 votes |
private String normaliseIPv6Address(final String hostname) { if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) { return hostname; } try { final InetAddress inetAddress = InetAddress.getByName(hostname); return inetAddress.getHostAddress(); } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above log.error("Unexpected error converting "+hostname, uhe); return hostname; } }
Example #21
Source File: Utils.java From stocator with Apache License 2.0 | 5 votes |
public static boolean validContainer(String container) throws InvalidContainerNameException { if (container != null && container.length() < 4) { throw new InvalidContainerNameException("Container " + container + " length must be at least 3 letters"); } if (InetAddressUtils.isIPv4Address(container)) { throw new InvalidContainerNameException("Container " + container + " is of IP address pattern"); } return true; }
Example #22
Source File: RLNetUtil.java From Roid-Library with Apache License 2.0 | 5 votes |
/** * Get IP address from first non-localhost interface * * @param ipv4 true=return ipv4, false=return ipv6 * @return address or empty string */ public static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 // port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception e) { e.printStackTrace(); } return ""; }
Example #23
Source File: Utilities.java From balanced-android with MIT License | 5 votes |
/** * Get IP address from first non-localhost interface * * @param useIPv4 true=return ipv4 address, false=return ipv6 address * @return address or empty string */ protected static String getIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) { return sAddr; } } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) {} // ignore exceptions return ""; }
Example #24
Source File: StreamCameraActivity.java From peepers with Apache License 2.0 | 5 votes |
private static String tryGetIpV4Address() { try { final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { final NetworkInterface intf = en.nextElement(); final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); while (enumIpAddr.hasMoreElements()) { final InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { final String addr = inetAddress.getHostAddress().toUpperCase(); if (InetAddressUtils.isIPv4Address(addr)) { return addr; } } // if } // while } // for } // try catch (final Exception e) { // Ignore } // catch return null; }
Example #25
Source File: AbstractVerifierDef.java From steady with Apache License 2.0 | 5 votes |
private String normaliseIPv6Address(final String hostname) { if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) { return hostname; } try { final InetAddress inetAddress = InetAddress.getByName(hostname); return inetAddress.getHostAddress(); } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above log.error("Unexpected error converting "+hostname, uhe); return hostname; } }
Example #26
Source File: AbstractCommonHostnameVerifierDef.java From steady with Apache License 2.0 | 5 votes |
private String normaliseIPv6Address(final String hostname) { if (hostname == null || !InetAddressUtils.isIPv6Address(hostname)) { return hostname; } try { final InetAddress inetAddress = InetAddress.getByName(hostname); return inetAddress.getHostAddress(); } catch (final UnknownHostException uhe) { // Should not happen, because we check for IPv6 address above log.error("Unexpected error converting "+hostname, uhe); return hostname; } }
Example #27
Source File: J_AbstractVerifier_V.java From steady with Apache License 2.0 | 4 votes |
private static boolean isIPAddress(final String hostname) { return hostname != null && (InetAddressUtils.isIPv4Address(hostname) || InetAddressUtils.isIPv6Address(hostname)); }
Example #28
Source File: URIBuilder.java From RoboZombie with Apache License 2.0 | 4 votes |
private String buildString() { StringBuilder sb = new StringBuilder(); if (this.scheme != null) { sb.append(this.scheme).append(':'); } if (this.encodedSchemeSpecificPart != null) { sb.append(this.encodedSchemeSpecificPart); } else { if (this.encodedAuthority != null) { sb.append("//").append(this.encodedAuthority); } else if (this.host != null) { sb.append("//"); if (this.encodedUserInfo != null) { sb.append(this.encodedUserInfo).append("@"); } else if (this.userInfo != null) { sb.append(encodeUserInfo(this.userInfo)).append("@"); } if (InetAddressUtils.isIPv6Address(this.host)) { sb.append("[").append(this.host).append("]"); } else { sb.append(this.host); } if (this.port >= 0) { sb.append(":").append(this.port); } } if (this.encodedPath != null) { sb.append(normalizePath(this.encodedPath)); } else if (this.path != null) { sb.append(encodePath(normalizePath(this.path))); } if (this.encodedQuery != null) { sb.append("?").append(this.encodedQuery); } else if (this.queryParams != null) { sb.append("?").append(encodeQuery(this.queryParams)); } } if (this.encodedFragment != null) { sb.append("#").append(this.encodedFragment); } else if (this.fragment != null) { sb.append("#").append(encodeFragment(this.fragment)); } return sb.toString(); }
Example #29
Source File: PrivacyEnforcementService.java From prebid-server-java with Apache License 2.0 | 4 votes |
/** * Masks ip v6 address by replacing last number of groups . */ private static String maskIpv6(String ip, Integer groupsNumber) { return ip != null && InetAddressUtils.isIPv6Address(ip) ? maskIp(ip, ":", groupsNumber) : ip; }
Example #30
Source File: ConsulDiscoveryClientCustomizedTests.java From spring-cloud-consul with Apache License 2.0 | 4 votes |
private void assertNotIpAddress(ServiceInstance instance) { assertThat(InetAddressUtils.isIPv4Address(instance.getHost())) .as("host is an ip address").isFalse(); }