rrr ppp
rrr ppp

Reputation: 671

Build URL in java

Trying to build http://IP:4567/foldername/1234?abc=xyz. I don't know much about it but I wrote below code from searching from google:

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

public class MyUrlConstruct {

    public static void main(String a[]){

        try {
            String protocol = "http";
            String host = "IP";
            int port = 4567;
            String path = "foldername/1234";
            URL url = new URL (protocol, host, port, path);
            System.out.println(url.toString()+"?");
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }
}

I am able to build URL http://IP:port/foldername/1234?. I am stuck at query part. Please help me to move forward.

Upvotes: 62

Views: 156517

Answers (6)

Saikat
Saikat

Reputation: 16750

Using Spring UriComponentsBuilder:

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.

Another example:

UriComponentsBuilder.fromHttpUrl(baseUrl)
  .path(path)
  .query(query)
  .build(id); // replaces placeholder variables

Read more ...

JavaDoc

Upvotes: 15

Tyler Liu
Tyler Liu

Reputation: 20356

Use OkHttp

There is a very popular library named OkHttp which has been starred 45K 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();

Upvotes: 39

android developer
android developer

Reputation: 116352

If you use Android, you can use the Uri.Builder API. Example:

val uri = Uri.Builder().scheme("https").authority("s3.amazonaws.com").appendEncodedPath(bucketName).appendEncodedPath(fileName).build()

Docs:

https://developer.android.com/reference/android/net/Uri.Builder

Upvotes: 1

vsminkov
vsminkov

Reputation: 11250

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();

Upvotes: 79

Philippe
Philippe

Reputation: 4158

If you happen to be using Spring already, I have found the org.springframework.web.util.UriComponentsBuilder to be quite nifty. Here is how you would use it in your case.

final URL myUrl = UriComponentsBuilder
        .fromHttpUrl("http://IP:4567/foldername/1234?abc=xyz")
        .build()
        .toUri()
        .toURL();

Upvotes: 13

VGR
VGR

Reputation: 44328

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.

Upvotes: 33

Related Questions