okhttp 客户端在 # 符号处截断 http URL [英] okhttp client truncates http URL at # sign

查看:97
本文介绍了okhttp 客户端在 # 符号处截断 http URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 okhttp 客户端(版本 3.10.0)调用 Bing 服务.测试中使用的地址带有#号.

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

1177 公园大道 5 号 #190橙园佛罗里达州 320734150美国

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

http 客户端在 # 符号处截断地址组件.通过代码构造的网址是

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

代码片段如下所示

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

如果我用单词suite替换#,那么构造的url是正确的

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

有没有其他方法可以让 # 构造的 url 不被截断?

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

推荐答案

某些符号(包括#")不是有效的 URL 符号.如果您的 URL 需要包含它们,则需要对其进行编码.因此,获取您的原始 URL 字符串并使用方法 URLEncoder.encode(...) 对其进行编码 这是 URLEncoder

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

这篇关于okhttp 客户端在 # 符号处截断 http URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆