Illegal character in query at index 177

Tags: , ,

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(;
HttpResponse httpResponse = httpclient.execute(httpGet);

This is the nextLink url:

“$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:

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.

Source: stackoverflow