Apache HttpGet url encoding problem (plus sign, +) - java

I'm sending a GET request with HttpClient but the + is not encoded.
1.
If I pass the query parameter string unencoded like this
URI uri = new URI(scheme, host, path, query, null);
HttpGet get = new HttpGet(uri);
Then the + sign is not encoded and it is received as a space on the server. The rest of the url is encoded fine.
2.If I encode the parameters in the query string like this
param = URLEncoder.encode(param,"UTF-8");
Then I get a bunch of weird symbols on the server, probably because the url has been encoded twice.
3.If I only replace the + with %B2 like this
query = query.replaceAll("\\+","%B2");
Then %B2 is encoded when the GET is executed by HttpClient
How can I properly encode Get parameters with Apache HttpClient and make sure the + is encoded as well?

Ok, the solution was that instead of creating the URI like this
URI uri = new URI(scheme, host, path, query, null);
One should create it like this
URIUtils.createURI(scheme, host, -1, path, query, null);
The purpose of the URIUtils class is
A collection of utilities for URIs, to workaround bugs within the
class
no comment........

When you build the query string, use URLEncoder.encode(paramValue, "UTF-8") for each parameter value. Then when you send the request, use URLDecoder.decode(paramValue, "UTF-8") and the "weird symbols" will be decoded.

Related

QueryParam decode my url encoded with cp1252

I have a get request with this param in the url (encoded in cp1252):
?c=es&t=a%20coru%F1a
I have a Spring Boot service with a QueryParam that automatically converts to:
a coru�a
The %20 are replaced by spaces and the %F1 are replace by �
If I try to encode again:
java.net.URLEncoder.encode(t, "Windows-1252");
This is the final result (%3F instead %F1)
a+coru%3Fa
What I need is the QueryParam doesn't decode the url, I only want that string as a I send it.
If I try with POST request and x-www-form-urlencoded everything works fine (obviously), but I need GET request.
This is what i did it at the end:
I've changed #RequestParam by:
HttpServletRequest request
And then:
request.getQueryString()
And I can get the values from the query url in the same format.
Probably it's not the best option, but it works!
Thanks.

java.net.URISyntaxException: Illegal character in query at index 177

I tried to get Azure Usage details via nextLink which is shared by Azure. while i tried to make http request URISyntaxException is occured.
HttpClient httpclient = getHttpClient();
URIBuilder uriBuilder=new URIBuilder(url);
HttpGet httpGet = new HttpGet(uriBuilder.build());
HttpResponse httpResponse = httpclient.execute(httpGet);
This is the nextLink url:
"https://management.azure.com/subscriptions/78c50b17-61fd-40cc-819c-4953586c7850/providers/Microsoft.Consumption/usageDetails?api-version=2019-11-01&$filter=properties/usageStart eq '2020-07-1' and properties/usageEnd eq '2020-07-30' &metric=actualcost&$expand=properties/meterDetails,properties/additionalInfo&sessiontoken=15:785628&$skiptoken=827CDTHDWI07C46616C7365730&skiptokenver=v1&id=2d790-d675-45d-89j56-3989w06cca"
I think this is because of characters such as ?, & and ! in my URL. so I tried using:
URLEncoder.encode(myUrl, "UTF-8");
but after this, I faced protocol exception.
Am I missing something here?
Your URL contains spaces and single quotes, these should be URL encoded like you tried. However, because you tried to URL-encode the entire URL, you end up with this:
https%3A%2F%2Fmanagement.azure.com%2Fsubscriptions%2F78c50b17-61fd-40cc-819c-4953586c7850%2Fproviders%2FMicrosoft.Consumption%2FusageDetails%3Fapi-version%3D2019-11-01%26%24filter%3Dproperties%2FusageStart+eq+%272020-07-1%27+and+properties%2FusageEnd+eq+%272020-07-30%27+%26metric%3Dactualcost%26%24expand%3Dproperties%2FmeterDetails%2Cproperties%2FadditionalInfo%26sessiontoken%3D15%3A785628%26%24skiptoken%3D827CDTHDWI07C46616C7365730%26skiptokenver%3Dv1%26id%3D2d790-d675-45d-89j56-3989w06cca
Which is not a valid URL. You could simply try using a naive form of String replacement:
myUrl = myUrl.replace(" ", "%20").replace("'", "%27");
If that is not sufficient, you'll need to reconstruct the URL yourself, and only apply URL-encoding on the query parameter values.

Url encoding issue with Jersey Client

I need to make a service call such as this:
http://myservice.com/path?var1=value1&var2=value2
The issue I have is value1 and value2 ends up getting encoded, and this makes the service call fail. For example, value1 is something like "a=b&b=c;2&&="... it contains special characters, basically.
I am guessing that this is an issue for the service to fix - to properly handle decoding encoded characters, which I do not think it is currently doing.
Here is a sample of how I am making these requests:
WebTarget target = client.target("http://test.com")
.path("path1")
.queryParam("var1", var1);
Builder builder = target.request();
...
What's puzzling to me is that if I make the same request just using Chrome, everything works. So that makes me to believe that I should have some way with the Jersey API of "disabling" the encoding.
Only way I have found so far to use "raw" Url is to use URI.
So call like this
URI uri = URI.create("http://localhost/~Common~0#/edit?vadf&&sfs&&fdsfd=fs&fsd");
WebTarget target = client.target(uri);
You get request url
1 > GET http://localhost/~Common~0#/edit?vadf&&sfs&&fdsfd=fs&fsd
Everything else I tried resulted in encoding special characters.

"Illegal Characters" in URL for HttpGet in Android get double-encoded

I am trying to find a solution to this the whole evening now...
I write an app which requests data from a web server. The Server answers in JSON format.
Everything works well except when I enter a umlaut like ä into my App.
In the following I assume the request URL is http://example.com/?q= and I am searching for "Jäger"
The correct call would then be h++p://example.com/?q=J%C3%A4ger
(Sorry for plus-signs but the spam protection doesnt let me post it correctly.)
So my problem is now:
When I give my URL String encoded or unencoded over to HttpGet it will always result in a doublee-encoded URL.
The Request to my Server is then http://example.com/?q=J%25C3%25A4ger (It encodes the percent signs)
which leads to the server searching in database for J%C3%A4ger what is obviously wrong.
So my question is how can I achive that if the user enters "Jäger" my app calls the correctly encoded URL?
Thanks for any help!
Here is the currently used code... Ist probably the worst possible idea I had...
URI url = new URI("http", "//example.com/?q=" + ((EditText)findViewById(R.id.input)).getText().toString(), null);
Log.v("MyLogTag", "API Request: " + url);
HttpGet httpGetRequest = new HttpGet(url);
// Execute the request in the client
HttpResponse httpResponse;
httpResponse = defaultClient.execute(httpGetRequest);
Update: Sorry, HttpParams isn't meant for request parameters but for configuring HttpClient.
On Android, you might want to use Uri.Builder, like suggested in this other SO answer:
Uri uri = new Uri.Builder()
.scheme("http")
.authority("example.com")
.path("someservlet")
.appendQueryParameter("param1", foo)
.appendQueryParameter("param2", bar)
.build();
HttpGet request = new HttpGet(uri.toString());
// This looks very tempting but does NOT set request parameters
// but just HttpClient configuration parameters:
// HttpParams params = new BasicHttpParams();
// params.setParameter("q", query);
// request.setParams(params);
HttpResponse response = defaultClient.execute(request);
String json = EntityUtils.toString(response.getEntity());
Outside of Android, your best bet is building the query string manually (with all the encoding hassles) or finding something similar to Android's Uri.Builder.

Why can't Google Maps static API handle my encoded URIs?

I'm trying to integrate some Google maps bits into my Java web app using the Google Maps static API. For the moment I'm just trying to get a map, any map. Their example:
http://maps.google.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=12&size=400x400&sensor=false
works fine from my browser. However the Java HTTP client software I am using (the one from Apache's http components, version 4.0.2) insists that I encode my URIs, so I end up with this:
http://maps.google.com/maps/api/staticmap?center%3D40.714728%2C-73.998672%26zoom%3D12%26size%3D400x400%26sensor%3Dfalse
which doesn't work. I would happily not encode my URIs but the Apache client fails if I don't. So my question is how can I either:
persuade Apache's client to use the plain URI or
get the encoded URI into a form Google will accept
?
Only encode the parameters of the URI. Your first ?, then your = and your & shouldn't be URI encoded.
Your URI should be
http://maps.google.com/maps/api/staticmap?center=40.714728%2C-73.998672&zoom=12&size=400x400&sensor=false
The only URI-encoded character is %2C, the , between your coordinates.
As Pekka suggested you need to leave to & and = unencoded.
Your encoded url
http://maps.google.com/maps/api/staticmap?center%3D40.714728%2C-73.998672%26zoom%3D12%26size%3D400x400%26sensor%3Dfalse
vs unencoded & (%26) and = (%3D) (working)
http://maps.google.com/maps/api/staticmap?center=40.714728%2C-73.998672&zoom=12&size=400x400&sensor=false
Apache's HTTPComponents HTTP Client has a lot of interfaces with which you can build your request URL. To make sure that the proper parts of the URL are encoded I would suggest using this method:
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("center", "40.714728,-73.998672"));
qparams.add(new BasicNameValuePair("zoom", "12"));
qparams.add(new BasicNameValuePair("size", "400x400"));
qparams.add(new BasicNameValuePair("sensor", "false"));
URI uri = URIUtils.createURI("http", "maps.google.com", -1, "/maps/api/staticmap",
URLEncodedUtils.format(qparams, "UTF-8"), null);
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
More example http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html
API Docs http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html

Categories