Problem retrieving a port number from URL.getPort () when the URL contains "]"

I use java.net.URL.getPort () to extract the port number from the URL. Most of the time this works great. However, when the URL contains the right-bracket character "]", it fails:

new URL("http://abc.com:123/abc.mp3").getPort(); returns: (int) 123 

But if the URL contains "]", I get:

 new URL("http://abc.com:123/abc].mp3").getPort(); returns: (int) -1 

What am I doing wrong?

EDIT # 1: As a test, I inserted the same code into an application other than Android Java, and the port number was correctly returned, so this looks anomalous with the Android SDK.

+4
source share
5 answers

If your URL contains some characters that are not valid in the URLs, you should use a URL encoded string. They seem to do this in Java using a URI .

 new URI( "http", null, "abc.com", 123, "abc].mp3", null, null).toURL().getPort(); 

If you already have a URL string:

 URL url = new URL("http://abc.com:123/abc].mp3"); 

Then this works for me:

 new URI( url.getProtocol(), null, url.getHost(), url.getPort(), url.getPath(), null, null); 

But then again, I'm using url.getPort() , which, as you said, does not work. But when I test on Java 6 now. new URL("http://abc.com:123/abc].mp3").getPort(); really works for me, maybe it's just on android, it doesn't work? If this does not work, I think it is better to use a third-party library for this. The Apache Http client, which is included in Android, seems to have extra functionality for URLs: see org.apache.http.client.utils

See also URL Encoding of an HTTP Address in Java

+4
source
 "http://abc.com:123/abc].mp3" 

] not allowed in part of the URI path, so this is not a URL. However, you can change the regular expression in the specification to get this information:

  //TODO: import java.util.regex.*; String expr = "^(([^:/?#]+):)?(//([^:/?#]*):([\\d]*))?"; Matcher matcher = Pattern.compile(expr) .matcher("http://abc.com:123/abc].mp3"); if (matcher.find()) { String port = matcher.group(5); System.out.println(port); } 

Despite the name, URLEncoder does not encode URLs. It should only be used to encode parameters in the query part when the server is waiting for application/x-www-form-urlencoded encoded data. The URI and URL classes behave as documented - they will not help you here.

+2
source

According to RFC1738, the symbol ] unsafe:

Other characters are unsafe because it is known that gateways and other transport agents sometimes modify such characters. These characters are "{", "}", "|", "\", "^", "~", "[", "]" and "` ".

Thus, only alphanumeric characters, special characters "$ -_. +! * '()," And reserved characters used for their reserved purposes can be used without restriction in the URL.

You must encode either the single character you want to add or run the whole line through the URL encoder. Try the following:

 new URL("http://abc.com:123/abc%5D.mp3").getPort(); 
+1
source

String encodedURL = new URI ("http", null, "//abc.com:8080/abc[dapter.jpg", null, null) .toASCIIString ();

0
source

Here is an easier way to extract a port from URLs that may be different from HTTP, for example. JNDI Connection URLs:

 int port = 80; // assumption of default port in the URL Pattern p = Pattern.compile(":\\d+"); // look for the first occurrence of colon followed by a number Matcher matcher = p.matcher(urlSrtr); if (matcher.find()) { String portStrWithColon = matcher.group(); if (portStrWithColon.length() > 1) { String portStr = portStrWithColon.substring(1); try { port = Integer.parseInt(portStr); } catch (NumberFormatException e) { // handle } } } return port; 
0
source

Source: https://habr.com/ru/post/1337615/


All Articles