3

我需要用 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

但它给出了一个例外。因此我也尝试使用 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> 
4

2 回答 2

4

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

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

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

以下是一些替代方案:

最后但并非最不重要:

PS:您的网址中的“>”怎么样?

于 2013-07-15T04:05:45.637 回答
2

谢谢保罗,

我已经通读并为此进行了另一轮研究,我正在使用

String url ="http://someurl.com/passfail?parameter={"data1":"123456789","data2":"123456789"}";
String encodedURL = URIUtil.encodeQuery(url);

它给了我状态 200,这是成功的。

我使用的 API 来自org.apache.commons.httpclient.util.URIUtil

于 2013-07-15T07:49:15.403 回答