简体   繁体   English

播放框架WS url空格

[英]Play framework WS url spaces

I have a problem calling WS.url() in play framework 2.3.3 with url containing spaces. 我在播放框架2.3.3中使用包含空格的url调用WS.url()时遇到问题。 All other characters all url encoded automatically but not spaces. 所有其他字符都自动编码,但不是空格。 When i try to change all spaces to "%20", WS convert it to "%2520" because of "%" character. 当我尝试将所有空格更改为“%20”时,由于“%”字符,WS将其转换为“%2520”。 With spaces i've got java.net.URISyntaxException: Illegal character in query. 使用空格我有java.net.URISyntaxException:查询中的非法字符。 How can i handle this ? 我怎么处理这个?

part of the URL's query String: URL的一部分查询字符串:

 &input=/mnt/mp3/music/folder/01 - 23.mp3

The code looks like this: 代码如下所示:

Promise<JsonNode> jsonPromise = WS.url(url).setAuth("", "cube", WSAuthScheme.BASIC).get().map(
                new Function<WSResponse, JsonNode>() {
                    public JsonNode apply(WSResponse response) {
                        System.out.println(response.getBody());
                        JsonNode json = response.asJson();
                        return json;
                    }
                }
                );

You should "build" your URL based on the way java.net.URL(which Play! uses for it's WS) does it. 您应该根据java.net.URL(Play!用于它的WS)的方式“构建”您的URL。 WS.url() follows the same logic. WS.url()遵循相同的逻辑。

The use of URLEncoder/Decoder is recommended only for form data. 建议仅对表单数据使用URLEncoder / Decoder。 From JavaDoc: 来自JavaDoc:

"Note, the java.net.URI class does perform escaping of its component fields in certain circumstances. The recommended way to manage the encoding and decoding of URLs is to use java.net.URI, and to convert “注意,java.net.URI类在某些情况下会执行其组件字段的转义。管理URL编码和解码的推荐方法是使用java.net.URI,并进行转换
between these two classes using toURI() and URI.toURL(). 使用toURI()和URI.toURL()在这两个类之间。 The URLEncoder and URLDecoder classes can also be used, but only for HTML form encoding, which is not the same as the encoding scheme defined in RFC2396." 也可以使用URLEncoder和URLDecoder类,但仅用于HTML表单编码,这与RFC2396中定义的编码方案不同。“

So, the solution is to use THIS : 所以,解决方案是使用这个

WS.url(baseURL).setQueryString(yourQueryString);

Where: 哪里:

  1. baseURL is your scheme + host + path etc. baseURL是你的方案+主机+路径等。
  2. yourQueryString is... well, your query String, but WITHOUT the ? yourQueryString是......好吧,你的查询字符串,但没有 ? : input=/mnt/mp3/music/folder/01 - 23.mp3 :input = / mnt / mp3 / music / folder / 01 - 23.mp3

Or, if you want to use a more flexible, programmatic approach, THIS : 或者,如果你想使用更灵活,编程方法,

WS.url(baseURL).setQueryParameter(param, value);

Where: 哪里:

  1. param is the parameter's name in the query String param是查询String中的参数名称
  2. value is the value of the parameter value是参数的值

If you want multiple parameters with values in your query you need to chain them by adding another .setQueryParameter(...) . 如果您想在查询中使用带有值的多个参数,则需要通过添加另一个.setQueryParameter(...)来链接它们。 This implies that this approach is not very accomodating for complex, multi-parameter query Strings. 这意味着这种方法不适合复杂的多参数查询字符串。


Cheers! 干杯!

If you check the console you will find that the exception is : java.net.URISyntaxException: Illegal character in path at index ... 如果你检查控制台,你会发现异常是: java.net.URISyntaxException: Illegal character in path at index ...

That's because play Java api uses java.net.URL (as you can see here in line 47). 这是因为可以玩Java API使用java.net.URL (你可以看到在这里的第47行)。

You can use java.net.URLEncoder to encode your URL 您可以使用java.net.URLEncoder对您的URL进行编码

WS.url("http://" + java.net.URLEncoder.encode("google.com/test me", "UTF-8"))

UPDATE UPDATE

If you want an RFC 2396 compliant method you can do this : 如果您需要符合RFC 2396的方法,则可以执行以下操作:

java.net.URI u = new java.net.URI(null, null, "http://google.com/test me",null);
System.out.println("encoded url " + u.toASCIIString()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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