Skip to content
Advertisement

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.

JavaScript

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?

Advertisement

Answer

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:

JavaScript

Which is not a valid URL. You could simply try using a naive form of String replacement:

JavaScript

If that is not sufficient, you’ll need to reconstruct the URL yourself, and only apply URL-encoding on the query parameter values.

Advertisement