2015-02-17 9 views
5

Ich habe ein seltsames Verhalten dieser Methode:java.net.URI bekommen Gastgeber mit Unterstrichen

import java.net.URI

URI url = new URI("https://pmi_artifacts_prod.s3.amazonaws.com"); 
    System.out.println(url.getHost()); /returns NULL 
    URI url2 = new URI("https://s3.amazonaws.com"); 
    System.out.println(url2.getHost()); //returns s3.amazonaws.com 

`

ich will erste url.getHost() pmi_artifacts_prod.s3.amazonaws sein. com, aber es gibt mir NULL. Es stellte sich heraus, dass das Problem mit den Unterstrichen im Domain-Namen auftrat, es ist ein bekannter Fehler, aber was kann ich tun, um genau mit diesem Host arbeiten zu können?

Antwort

0

https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_hostnames

public static void main(String...a) throws URISyntaxException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { 
    URI url = new URI("https://pmi_artifacts_prod.s3.amazonaws.com"); 
    System.out.println(url.getHost()); //NULL 


    URI uriObj = new URI("https://pmi_artifacts_prod.s3.amazonaws.com"); 
    if (uriObj.getHost() == null) { 
     final Field hostField = URI.class.getDeclaredField("host"); 
     hostField.setAccessible(true); 
     hostField.set(uriObj, "pmi_artifacts_prod.s3.amazonaws.com"); 
    } 
    System.out.println(uriObj.getHost()); //pmi_artifacts_prod.s3.amazonaws.com 


    URI url2 = new URI("https://s3.amazonaws.com"); 
    System.out.println(url2.getHost()); //s3.amazonaws.com 
} 
+2

Bitte explane Ihre (gut) Antwort. – Hannes

+1

"Seien Sie vorsichtig in dem, was Sie aussenden, seien Sie liberal in dem, was Sie akzeptieren." Wenn Menschen Unterstriche in ihren Hostnamen einfügen, sollte eine Bibliothek, die weltweit verwendet wird, mit ihnen umgehen, nicht daran scheitern. Einfach nicht robust und erschreckend schlecht für eine Sprache wie Java. –

-1

Nein, wird das Ergebnis absolut kommen richtig. gehen freundlicherweise durch die Dokumentation für url.getHost()

Java-Dokumentation sagt :

The host component of a URI, if defined, will have one of the following forms: 

    A domain name consisting of one or more labels separated by period characters ('.'), optionally followed by a period character. Each label consists of alphanum characters as well as hyphen characters ('-'), though hyphens never occur as the first or last characters in a label. The rightmost label of a domain name consisting of two or more labels, begins with an alpha character. 

    A dotted-quad IPv4 address of the form digit+.digit+.digit+.digit+, where no digit sequence is longer than three characters and no sequence has a value larger than 255. 

    An IPv6 address enclosed in square brackets ('[' and ']') and consisting of hexadecimal digits, colon characters (':'), and possibly an embedded IPv4 address. The full syntax of IPv6 addresses is specified in RFC 2373: IPv6 Addressing Architecture. 

    The host component of a URI cannot contain escaped octets, hence this method does not perform any decoding. 
    Returns: 
    The host component of this URI, or null if the host is undefined 

zu dieser Antwort hinzuzufügen, wenn mit Eclipse bitte an der Methodendefinition (drücken Sie f3) springen Sie Ihre Antwort zu bekommen.

1

Unders Unterstützung rechts in die URI hinzugefügt werden könnten durch das Patchen:

public static void main(String[] args) throws Exception { 
    patchUriField("lowMask", "L_DASH"); 
    patchUriField("highMask", "H_DASH"); 

    URI s = URI.create("http://my_favorite_host:3892"); 
    // prints "my_favorite_host" 
    System.out.println(s.getHost()); 
} 

private static void patchUriField(String methodName, String fieldName) 
     throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException { 
    Method lowMask = URI.class.getDeclaredMethod(methodName, String.class); 
    lowMask.setAccessible(true); 
    long lowMaskValue = (long) lowMask.invoke(null, "-_"); 

    Field lowDash = URI.class.getDeclaredField(fieldName); 

    Field modifiers = Field.class.getDeclaredField("modifiers"); 
    modifiers.setAccessible(true); 
    modifiers.setInt(lowDash, lowDash.getModifiers() & ~Modifier.FINAL); 

    lowDash.setAccessible(true); 
    lowDash.setLong(null, lowMaskValue); 
} 
Verwandte Themen