gpt4 book ai didi

java - 使用 JSON 字符串解析 URL 时捕获 IllegalArgumentException

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:48:44 24 4
gpt4 key购买 nike

我需要用 JSON 数据制定一个 url,看起来像

http://someurl.com/passfail?parameter= {"data1":"123456789","data2":"123456789"},我需要使用 JBoss 的 ClientResponse 传递它以获得响应状态。我首先尝试在

中传递文字字符串数据
ClientRequest clientrequest = new ClientRequest("http://someurl.com/passfail?parameter={\"data1\":\"123456789\",\"data2\":\"123456789\"});// assuming the "\" is formulated correctly

但它给出了一个异常(exception)。因此,我也尝试使用 URL url = new URL(the url),但效果不佳。

我在尝试时发现了以下异常,我感到很困惑,希望有人能提供帮助。

非法参数异常:

org.jboss.resteasy.specimpl.UriBuilderImpl.buildFromMap(UriBuilderImpl.java:408)> 
org.jboss.resteasy.specimpl.UriBuilderImpl.buildFromValues(UriBuilderImpl.java:558)>
org.jboss.resteasy.specimpl.UriBuilderImpl.build(UriBuilderImpl.java:539)>
org.jboss.resteasy.client.ClientRequest.getUri(ClientRequest.java:786)>
org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor.execute(ApacheHttpClientExecutor.java:77)>
org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:39)>
org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPInterceptor.execute(AcceptEncodingGZIPInterceptor.java:40)>
org.jboss.resteasy.core.interception.ClientExecutionContextImpl.proceed(ClientExecutionContextImpl.java:45)>
org.jboss.resteasy.client.ClientRequest.execute(ClientRequest.java:473)>
org.jboss.resteasy.client.ClientRequest.httpMethod(ClientRequest.java:704)>
org.jboss.resteasy.client.ClientRequest.get(ClientRequest.java:509)>
org.jboss.resteasy.client.ClientRequest.get(ClientRequest.java:537)>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX at line personal Servlet
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX at line personal Servlet
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)>
weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)>
weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)>
weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)>
weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)>
weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:505)>
weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:251)>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX at line personal Servlet JSP
weblogic.servlet.jsp.JspBase.service(JspBase.java:34)>
weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)>
weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)>
weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)>
weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)>
weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:505)>
weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:251)>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX at line personal Servlet
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX at line personal Servlet
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)>
weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)>
weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)>
weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)>
weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)>
weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:505)>
weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:251)>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX at line personal Servlet
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX at line personal Servlet
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)>
weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)>
weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)>
weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)>
weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)>
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3498)>
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)>
weblogic.security.service.SecurityManager.runAs(Unknown Source)>
weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)>
weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)>
weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)>
weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)>
weblogic.work.ExecuteThread.run(ExecuteThread.java:173)>
Caused by: java.lang.IllegalArgumentException>
java.net.URI.create(URI.java:842)>
org.jboss.resteasy.specimpl.UriBuilderImpl.buildFromMap(UriBuilderImpl.java:404)>
... 60 more>
Caused by: java.net.URISyntaxException: Illegal character in query index 77: http://someurl.com/passfail?parameter={"data1":"123456789","data2":"123456789"}>
java.net.URI$Parser.fail(URI.java:2809)>
java.net.URI$Parser.checkChars(URI.java:2982)>
java.net.URI$Parser.parseHierarchical(URI.java:3072)>
java.net.URI$Parser.parse(URI.java:3014)>
java.net.URI.<init>(URI.java:578)>
java.net.URI.create(URI.java:840)>
... 61 more>

最佳答案

问题是您在 URI 字符串中传递了非法字符:Java - Convert String to valid URI object

http://someurl.com/passfail?parameter={"data1":"123456789","data2":"123456789"}>

您需要“转义”URI 中的违规字符。

这里有一些替代方案:

最后但同样重要的是:

PS:您的 URL 中的“>”怎么办?

关于java - 使用 JSON 字符串解析 URL 时捕获 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17646520/

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