简体   繁体   中英

okhttp client truncates http URL at # sign

I am using okhttp client (version 3.10.0) to call Bing service. The Address used in the test has # sign.

1177 PARK AVE STE 5 #190 ORANGE PARK FL 320734150 US

http client truncates the address component at # sign. URL constructed through the code is

https://dev.virtualearth.net/REST/v1/Locations/1177%20PARK%20AVE%20STE%205?%20maxresults=1&key=XXXXXXXXXXXXXXXXXXXXXXX&o=xml

Code fragment is shown below

Request.Builder builder = bingOkHttpClient.newRequestBuilder(path, buildQueryParams());
builder.addHeader("Accept","application/xml");
bingOkHttpClient.newCall(builder.build()).execute();

If i replace # with the word suite, then url contructed is correctly

https://dev.virtualearth.net/REST/v1/Location1s/177%20PARK%20AVE%20STE%205%20suite#20190%ORANGE%20PARK%20FL%320734150%US?maxresults=1&key=XXXXXXXXXXXXXXXXXXXXX&o=xml

Is there any other way to make the url constructed with # without getting truncated?

Some symbols (including '#') are not valid URL symbols. If your URL needs to include them they need to be encoded. So take your original URL string and encode it with method URLEncoder.encode(...) Here is Javadoc for URLEncoder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM