43

I am building a String with StringBuilder

StringBuilder builder = new StringBuilder();
builder.append("my parameters");
builder.append("other parameters");

Then i build a Url

Url url = new Url(builder.toString());

And then i try the connection

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

But the url seems not to be right from the results i get. It's like some parameter is being false passed. That's why i think the problem is in the part of the StringBuilder.

The problem is in a double parameter i try to pass.

double longitude = 23.433114;
String lng = String.ValueOf(longitude);

And then i put it in the url. But if i give it as a string the result is correct.

String lng = "23.433114"

Is UrlEncoding necessary? I will try what is suggested below.

Manolis Karamanis
  • 758
  • 1
  • 10
  • 27
  • You should be using a library which does URI Templates (see [here](https://github.com/fge/uri-template) for example) – fge Oct 29 '14 at 22:36
  • 1
    @RealSkeptic `URLEncoder.encode()` encodes for application/x-www-form-urlencoded`, which is quite different from encoding query parameters/URI fragments/etc – fge Oct 29 '14 at 22:38
  • 1
    @fge No, URLencode is a standard encoding scheme. It is used both for parameters in GET queries and for parameters in POST queries which are application/x-www-form-urlencoded. See [Wikipedia](http://en.wikipedia.org/wiki/Percent-encoding). – RealSkeptic Oct 29 '14 at 22:46
  • @fge It is exactly the same thing for parameter values. He didn't say anything about URI fragments. Parameter names should also be `URLEncoded.` – user207421 Oct 29 '14 at 22:47
  • 1
    @RealSkeptic @EJP sorry but that's wrong. In query parameters, for instance, a space becomes `%20`; as I said, the method you mention encodes for forms in which the space becomes `+`. And that is only one example – fge Oct 29 '14 at 22:48
  • @fge No, [a space becomes + in query parameters](http://www.w3.org/TR/html4/interact/forms.html#form-content-type), and that's what `URLEncoder` does. Despite its name it has nothing to do with URLs specifically. – user207421 Oct 29 '14 at 22:54
  • 1
    @fge - try both of them in a query. You'll be surprised. + Is an acceptable replacement for space in GET queries. But if you don't believe Wikipedia, Try [The HTML Spec](http://www.w3.org/TR/html4/interact/forms.html#h-17.13.3.3) – RealSkeptic Oct 29 '14 at 22:54
  • fge is correct. The first two sentences of the [documentation](http://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html) couldn't be clearer: “Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format.” Using URLEncoder for query parameters in a URL is wrong. I know it looks similar but it's not correct. The correct way to encode a query is with any java.net.URI constructor that takes more than one argument. – VGR Oct 29 '14 at 23:26
  • @VGR The requirement is the same whether it's a query parameter or a form parameter. – user207421 Oct 29 '14 at 23:39
  • @EJP forms.html in the W3C spec describes how to encode data submitted from an HTML form. However, data submitted for any other purpose (such as a REST service) should use URI encoding. – VGR Oct 29 '14 at 23:48
  • @EJP why do you quote the HTML spec when we are talking about URIs? – fge Oct 30 '14 at 06:22
  • @RealSkeptic why do you quote the HTML spec when we are talking about URIs? – fge Oct 30 '14 at 06:23
  • @fge Because we're talking about the way one builds a query string. The HTML spec describes exactly that, and tells you that it's done in the same way and using the same standard for both GET and PUT. – RealSkeptic Oct 30 '14 at 07:49
  • Possible duplicate of [What is the idiomatic way to compose a URL or URI in Java?](http://stackoverflow.com/questions/883136/what-is-the-idiomatic-way-to-compose-a-url-or-uri-in-java) – Nick Grealy Apr 13 '17 at 04:55

3 Answers3

98

Try apache's URIBuilder : [Documentation]

import org.apache.http.client.utils.URIBuilder;

// ...

URIBuilder b = new URIBuilder("http://example.com");
b.addParameter("t", "search");
b.addParameter("q", "apples");

Url url = b.build().toUrl();

Maven dependency:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.1</version>
</dependency>
Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
jhkuperus
  • 1,439
  • 11
  • 15
4

Since you want to create the URL and consume it through a GET request, it would be better to use a library that helps you in this process. You can use HttpComponents or another library like Unirest that is built on top of HttpComponents which ease all this work.

Here's an example using Unirest:

HttpResponse<String> stringResponse = Unirest.get("https://www.youtube.com/results")
    .field("search_query", "eñe")
    .asString();
System.out.println(stringResponse.getBody());

This will retrieve the HTML response corresponding to all the results from a search on youtube using "eñe". The ñ character will be encoded for you.

DISCLAIMER: I'm not attached to Unirest in any mean. I'm not a developer or a sponsor of this project. I'm only a happy user of this framework.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

And another option, this time using Spring framework (especially useful when doing some automated tests)

UriComponentsBuilder.newInstance()
                .scheme(URIScheme.HTTP.getId())
                .host("host")
                .port(8000)
                .path("/employee")
                .pathSegment("{employeeName}", "{anotherThing}")
                // this build(xxx) will automatically perform encoding
                .build("John", "YetMoreData")
                .toURL();
Tonsic
  • 890
  • 11
  • 15
  • The lillte problem with this tool is that not all special characters are encoded. For instance plus signs in query params are not encoded! – Ahmad Shahwan Jun 15 '23 at 12:24
  • Hum, didn't know that. Maybe this method does what you spoke about? https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html#encode() – Tonsic Jun 15 '23 at 21:36