URISyntaxException在将双引号””连接到URL时发生错误

huangapple go评论60阅读模式
英文:

URISyntaxException while concatenating double quotes " " to a URL

问题

我有以下代码:

public List<Attraction> retrieveByQuery(String query, int page, int size) throws IOException, InterruptedException {
    String URL = "http://mydomain/get-by-query?";
    URL += "query=" + query + "&page=" + page + "&size=" + size;

    HttpClient httpClient = HttpClient.newHttpClient();
    HttpRequest httpRequest = HttpRequest
            .newBuilder()
            .GET()
            .header("accept", "application/json")
            .uri(URI.create(URL))
            .build();

    HttpResponse<String> httpResponses = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());

    return null;
}

这个问题给我报了一个 URISyntaxException 错误。我认为问题出在 &quot;&quot;(双引号)上。

编辑

以下是带有异常和详细信息的完整堆栈跟踪:

在线程 "JavaFX Application Thread" 中出现异常 "java.lang.IllegalArgumentException: Illegal character in query at index 126: http://.../get-by-query?query=name=="Casa Lucio Dalla"&page=0&size=100
at java.base/java.net.URI.create(URI.java:883)
at dao_implementations.AttractionDAO_MongoDB.retrieveByQuery(AttractionDAO_MongoDB.java:119)
at controllers.CrudAttractionController.doRetrieveByQuery(CrudAttractionController.java:505)
...

随后是:

由于此查询中存在非法字符,导致异常 "java.net.URISyntaxException: Illegal character in query at index 126: http://.../get-by-query?query=name=="Casa Lucio Dalla"&page=0&size=100
at java.base/java.net.URI$Parser.fail(URI.java:2913)
at java.base/java.net.URI$Parser.checkChars(URI.java:3084)
at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3172)
...

请注意,如果我使用没有空格的查询,例如将 "Casa" 替换为 "Casa Lucio Dalla",则可以正常工作。

编辑2

如果我使用 URL += "query=" + URLEncoder.encode(query, StandardCharsets.UTF_8) + "&page=" + page + "&size=" + size;,会出现 500 内部服务器错误。

英文:

I have

public List&lt;Attraction&gt; retrieveByQuery(String query, int page, int size) throws IOException, InterruptedException {
        String URL = &quot;http://mydomain/get-by-query?&quot;;    
    URL += &quot;query=&quot; + query + &quot;&amp;page=&quot; + page + &quot;&amp;size=&quot; + size;

        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest httpRequest = HttpRequest
                .newBuilder()
                .GET()
                .header(&quot;accept&quot;, &quot;application/json&quot;)
                .uri(URI.create(URL)) // thows URISyntaxException
                .build();

        HttpResponse&lt;String&gt; httpResponses = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());


        return null;
    }
  • This is giving me
    > URISyntaxException

how can I fix? I think the problem is with &quot;&quot; (double quotes).

EDIT

This is the full stack strace with exceptions and details:

Exception in thread "JavaFX Application Thread"

java.lang.IllegalArgumentException: Illegal character in query at index 126: http://.../get-by-query?query=name==&quot;Casa Lucio Dalla&quot;&amp;page=0&amp;size=100
	at java.base/java.net.URI.create(URI.java:883)
	at dao_implementations.AttractionDAO_MongoDB.retrieveByQuery(AttractionDAO_MongoDB.java:119)
	at controllers.CrudAttractionController.doRetrieveByQuery(CrudAttractionController.java:505)
	at controllers.CrudAttractionController.lambda$buttonConfermaClicked$9(CrudAttractionController.java:451)
	at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
	at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
	at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
	at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
	at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
	at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
	at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
	at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
	at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
	at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
	at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
	at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
	at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
	at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
	at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
	at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
	at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
	at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.URISyntaxException: Illegal character in query at index 126: http://Troppadvisorserver-env.eba-pfsmp3kx.us-east-1.elasticbeanstalk.com/attraction/search-by-rsql-no-point?query=name==&#39;Casa Lucio Dalla&#39;;certificateOfExcellence==false&amp;page=0&amp;size=100
	at java.base/java.net.URI$Parser.fail(URI.java:2913)
	at java.base/java.net.URI$Parser.checkChars(URI.java:3084)
	at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3172)
	at java.base/java.net.URI$Parser.parse(URI.java:3114)
	at java.base/java.net.URI.&lt;init&gt;(URI.java:600)
	at java.base/java.net.URI.create(URI.java:881)
	... 49 more

followed by:

Caused by: java.net.URISyntaxException: Illegal character in query at index 126: http://.../get-by-query?query=name==&quot;Casa Lucio Dalla&quot;&amp;page=0&amp;size=100

Note that if I use a query with no spaces, for example Casa instead of Casa Lucio Dalla it works perfectly.

EDIT2

If I use URL += &quot;query=&quot; + URLEncoder.encode(query, StandardCharsets.UTF_8) + &quot;&amp;page=&quot; + page + &quot;&amp;size=&quot; + size;, it gives me Error 500 internal server error

答案1

得分: 2

你需要对URL的query部分进行URL编码。

如果URL中包含&quot;字符,则必须以%22的形式表示。

以下是一些代码:

import java.net.URLEncoder;

...

URL += "query=" + URLEncoder.encode(query, StandardCharsets.UTF_8) + "&amp;page=" + page + "&amp;size=" + size;
//                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

更新:

> 如果我使用_[上面的建议]_,会出现500内部服务器错误

这意味着URL被正确创建,并且请求已经达到服务器。如果你无法解决这个问题,请发布一个新的问题,附上服务器端的错误信息。

英文:

You need to URL encode the query part of the URL.

If the URL should contain a &quot; character, it must be in the form of %22.

Here's some code:

import java.net.URLEncoder;

...

URL += &quot;query=&quot; + URLEncoder.encode(query, StandardCharsets.UTF_8) + &quot;&amp;page=&quot; + page + &quot;&amp;size=&quot; + size;
//                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Update:

> If I use [the above suggestion], it gives me Error 500 internal server error

This means that the URL in created properly, and that the request reaches the server. Please post a new question with the server side error if you can't figure that out.

huangapple
  • 本文由 发表于 2020年9月14日 01:53:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63873883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定