12

I have a java.net.URL object that uses the HTTPS protocol, e.g.:

https://www.bla.com

And I have to change only the protocol part of this URL object so that when I call its toString() method I get this:

http://www.bla.com

What is the best way to do that?

0

5 Answers 5

17

You'll have the use the methods available to you:

URL oldUrl = new URL("https://www.bla.com");
URL newUrl = new URL("http", oldUrl.getHost(), oldUrl.getPort(), oldUrl.getFile(), oldUrl.getRef());

There's an even more expansive set() method that takes 8 items, you might need that for more elaborate URLs.

Edit: As was just pointed out to me, I wasn't paying attention, and set() is protected. So URL is technically mutable, but to us mortals, it's immutable. So you'll just have to construct a new URL object.

Sign up to request clarification or add additional context in comments.

4 Comments

The set() method is protected.
looks like you took the parameters of the URL.set() method for the constructor, but no such constructor with that prototype actually exists.
This is a wrong answer. There's no constructor with that signature. URL just stinks
This answer does not handle cases where the port is not specified and getPort() returns -1 so you have to rely on getDefaultPort()
5

You can also use string replacement:

URL oldUrl = new URL("https://www.bla.com");
String newUrlString = oldUrl.toString().replaceFirst("^https", "http");
URL newUrl = new URL(newUrlString);

Comments

3

String url - "https://www.some-host.com/some-path?somequery=some-value";

String newUrl = UriComponentsBuilder.newInstance()
          .fromUriString(url)
          .scheme("http")
          .build()
          .toUriString();

It'll just replace scheme and rest url will remain as-is

Comments

1

Or you can use org.springframework.web.util.UriComponentsBuilder/ org.springframework.web.util.UriComponents

see: Does there exist a mutable URL/URI object in Java?

Comments

1

It appears they forgot to make their class useful. Stealing code from URL.toExternalForm() helps:

public class ICantBelieveThisIsNotInTheStandardLibrary {
    public static final URL makeCopyWithDifferentProtocol(URL u, String protocol) {
        StringBuffer result = new StringBuffer();
        result.append(protocol);
        result.append(":");
        if (u.getAuthority() != null && u.getAuthority().length() > 0) {
            result.append("//");
            result.append(u.getAuthority());
        }
        if (u.getPath() != null) {
            result.append(u.getPath());
        }
        if (u.getQuery() != null) {
            result.append('?');
            result.append(u.getQuery());
        }
        if (u.getRef() != null) {
            result.append("#");
            result.append(u.getRef());
        }
        return new URL(result.toString());
    }
}

The answer which suggests to use a String-replacement is shorter, but you are going to need something like this if you ever want to change any of the other URL components.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.