How to change only Java net. Protocol part of URL object?
•
Java
I have a Java. Net that uses HTTPS protocol net. URL object, for example:
https://www.bla.com
I only need to change the protocol part of the URL object so that when I call its toString () method, I get this:
http://www.bla.com
What is the best way?
Solution
You will use the available methods:
URL oldUrl = new URL("https://www.bla.com"); URL newUrl = new URL("http",oldUrl.getHost(),oldUrl.getPort(),oldUrl.getFile(),oldUrl.getRef());
There is a broader set () method that requires eight items, and you may need a finer URL
Editor: as just pointed out to me, I didn't notice and set () was protected So the URL is technically changeable, but for us mortals, it is immutable So you just need to build a new URL object
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码