gpt4 book ai didi

java - URISyntaxException 查询中的非法字符

转载 作者:搜寻专家 更新时间:2023-11-01 02:47:35 28 4
gpt4 key购买 nike

我试图在默认浏览器中打开一个链接。我使用了以下代码。

String myUrl = "http://www.example.com/engine/myProcessor.jsp?Type=A Type&Name=1100110&Char=!"; 

try {
Desktop.getDesktop().browse(new URI(myUrl));
} catch (IOException err) {
setTxtOutput("Error: "+err.getMessage());
} catch (URISyntaxException err) {
setTxtOutput("Error: "+err.getMessage());
} catch (Exception err) {
setTxtOutput("Error: "+err.getMessage());
}

我得到 URISyntaxException Illegal character in query at index

我认为这是因为我的 URL 中包含 ?&! 等字符。我尝试使用:

URLEncoder.encode(myUrl, "UTF-8");

但这给了我另一个错误。

Failed to open http%3A%2F%2Fwww.example.com%2F........... 
The system cannot find the file specified.

请告诉我如何更正 URISyntaxException Illegal character 错误。

最佳答案

您不应该对整个 URL 进行编码,因为 URI 类需要有效的协议(protocol)。只对参数进行编码

String params = URLEncoder.encode("Type=A Type&Name=1100110&Char=!", "UTF-8");
myUrl = "http://www.example.com/engine/myProcessor.jsp?" + params;

关于java - URISyntaxException 查询中的非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18574154/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com