Build URL in java

We Are Going To Discuss About Build URL in java. So lets Start this Java Article.

Build URL in java

  1. Build URL in java

    There is a very popular library named OkHttp which has been starred 20K times on GitHub. With this library, you can build the url like below:
    import okhttp3.HttpUrl; URL url = new HttpUrl.Builder()

  2. Build URL in java

    There is a very popular library named OkHttp which has been starred 20K times on GitHub. With this library, you can build the url like below:
    import okhttp3.HttpUrl; URL url = new HttpUrl.Builder()

Solution 1

You can just pass raw spec

new URL("http://IP:4567/foldername/1234?abc=xyz");

Or you can take something like org.apache.http.client.utils.URIBuilder and build it in safe manner with proper url encoding

URIBuilder builder = new URIBuilder();
builder.setScheme("http");
builder.setHost("IP");
builder.setPath("/foldername/1234");
builder.addParameter("abc", "xyz");
URL url = builder.build().toURL();

Original Author vsminkov Of This Content

Solution 2

Use OkHttp

There is a very popular library named OkHttp which has been starred 20K times on GitHub. With this library, you can build the url like below:

import okhttp3.HttpUrl;

URL url = new HttpUrl.Builder()
    .scheme("http")
    .host("example.com")
    .port(4567)
    .addPathSegments("foldername/1234")
    .addQueryParameter("abc", "xyz")
    .build().url();

Or you can simply parse an URL:

URL url = HttpUrl.parse("http://example.com:4567/foldername/1234?abc=xyz").url();

Original Author Tyler Liu Of This Content

Solution 3

In general non-Java terms, a URL is a specialized type of URI. You can use the URI class (which is more modern than the venerable URL class, which has been around since Java 1.0) to create a URI more reliably, and you can convert it to a URL with the toURL method of URI:

String protocol = "http";
String host = "example.com";
int port = 4567;
String path = "/foldername/1234";
String auth = null;
String fragment = null;
URI uri = new URI(protocol, auth, host, port, path, query, fragment);
URL url = uri.toURL();

Note that the path needs to start with a slash.

Original Author VGR Of This Content

Solution 4

If using Spring Framework:

UriComponentsBuilder.newInstance()
  .scheme(scheme)
  .host(host)
  .path(path)
  .build()
  .toUri()
  .toURL();

A new UriComponentsBuilder class helps to create UriComponents
instances by providing fine-grained control over all aspects of
preparing a URI including construction, expansion from template
variables, and encoding.

Know more:
https://www.baeldung.com/spring-uricomponentsbuilder

JavaDoc:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html

Original Author Saikat Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment