IllegalArgumentException caught when parsing URL with JSON String

瘦欲@ 提交于 2019-12-10 21:33:03

问题


I need to formulate a url with JSON data that will look something like

http://someurl.com/passfail?parameter={"data1":"123456789","data2":"123456789"}, and I will need to pass it using JBoss's ClientResponse to get a response status. I first tried passing in the literal String data in

ClientRequest clientrequest = new ClientRequest("http://someurl.com/passfail?parameter={\"data1\":\"123456789\",\"data2\":\"123456789\"});// assuming the "\" is formulated correctly

but it gives an exception. Thus I also tried using URL url = new URL(the url) but it doesn't work as well.

I have the following exception caught trying and am feeling pretty stucked and am hoping if anyone could help.

IllegalArgumentException:

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> 

回答1:


The problem is that you're passing illegal characters in your URI string:Java - Convert String to valid URI object

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

You need to "escape" the offending characters in your URI.

Here are some alternatives:

  • How should I escape strings in JSON?

  • Java - Convert String to valid URI object

  • How do I encode URI parameter values?

  • Encoding URL query parameters in Java

  • http://docs.oracle.com/javase/6/docs/api/java/net/URI.html

and, last but not least:

  • java.net.URLEncoder

PS: what about that ">" in your URL?




回答2:


Thanks paul,

I've read through and do another round of research for this and I was using

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

and it gave me status 200, which is a success.

The API I used was from org.apache.commons.httpclient.util.URIUtil.



来源:https://stackoverflow.com/questions/17646520/illegalargumentexception-caught-when-parsing-url-with-json-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!