10

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

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非法字符

我认为这是因为诸如, &! 在我的网址中。我尝试使用:

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

但这给了我另一个错误。

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

请您告诉我如何更正URISyntaxException 非法字符错误。

4

6 回答 6

13

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

myUrl = "http://www.example.com/engine/myProcessor.jsp?" + URLEncoder.encode("Type=A Type&Name=1100110&Char=!", "UTF-8");
于 2013-09-02T13:13:10.937 回答
8

这是因为这里的空格...jsp?Type=A Type&...,您可以将其替换为+

http://www.example.com/engine/myProcessor.jsp?Type=A+Type&Name=1100110&Char=!"
于 2013-09-02T13:15:02.537 回答
2

您可以使用以下代码片段

public static String encode(String queryParameter) {
    try {
        String encodedQueryParameter = URLEncoder.encode(queryParameter, "UTF-8");
        return encodedQueryParameter;
    } catch (UnsupportedEncodingException e) {
        return "Issue while encoding" + e.getMessage();
    }
}
于 2020-03-12T12:47:19.060 回答
0

以下代码对我有用:

MultivaluedMap<String, String> params = new MultivaluedMapImpl();
            params.add("filter", URLEncoder.encode(filter));

_webResource.path(MessageFormat.format(this._methodName,this._params))
                        .queryParams((MultivaluedMap<String, String>) this._params)
                        .type("application/x-www-form-urlencoded")
                        .accept("application/json")
                        .get(ClientResponse.class); 
于 2018-07-17T17:50:38.977 回答
0

如果您使用任何过滤器参数,则不要在过滤器表达式之间使用空格。使用 %20 代替空格。例如,我的过滤器是这样的 _filter = Number_field eq 15 所以这在 JMeter 中不起作用,因为 Number 字段和 eq 之间有空格 像这样使用 _filter = Number_field%20eq%2015

于 2021-11-04T08:12:36.113 回答
-1

将 URL 中的空格替换为 + like 如果 url 包含 category=Adult Care,则将其替换为 category=Adult+Care。

于 2017-11-14T06:42:42.683 回答