1

I am trying to connect to a URL in java. But i am getting the below exception. I have tried to use the URLEncoder for encoding the URL in utf-8 but still i am getting the below Exception while trying to connect to the below URL:

URL:

String url = "http://bbb.org/classify/id?itemId=15652722&event=ITEM.RECLASSIFY&classifierName=ListingClassifier&appId=ListingClassifier&payload={\"source\":\"test\"}";

Exception:

Caused by: java.net.URISyntaxException: Illegal character in query at index 148: http://bbb.org/classify/id?itemId=15652722&event=ITEM.RECLASSIFY&classifierName=ListingClassifier&appId=ListingClassifier&payload={"source":"test"}

at java.net.URI$Parser.fail(URI.java:2848)

at java.net.URI$Parser.checkChars(URI.java:3021)

at java.net.URI$Parser.parseHierarchical(URI.java:3111)

at java.net.URI$Parser.parse(URI.java:3053)

at java.net.URI.<init>(URI.java:588)

at java.net.URI.create(URI.java:850)

Basically the exception is occurring at "{". Please suggest whats wrong with the URL.

Tried Encoding like below:

String url = "http://bbb.org/classify/id?itemId=15652722&event=ITEM.RECLASSIFY&classifierName=ListingClassifier&appId=ListingClassifier&payload={\"source\":\"test\"}";
url = URLEncoder.encode(url, "UTF-8"); 

But encoding didnt solve the issue.

5
  • Can you please post your code to know how you have encoded and use it Commented Jun 2, 2018 at 18:53
  • Probably a duplicate of stackoverflow.com/questions/10786042/… Commented Jun 2, 2018 at 18:57
  • You need to encode the string using URLEncoder, the error in the logs is complaining about the invalid character { Commented Jun 2, 2018 at 18:59
  • 1
    Possible duplicate of Java URL encoding of query string parameters Commented Jun 2, 2018 at 19:10
  • @Amit Bera i have encoded in the same way as in the answer by yahya. Commented Jun 2, 2018 at 21:26

1 Answer 1

2

You need to encode the URI to replace illegal characters (if any) with legal encoded characters.. something like this:

String url = "http://bbb.org/classify/id?itemId=15652722&event=ITEM.RECLASSIFY&classifierName=ListingClassifier&appId=ListingClassifier&payload={\"source\":\"test\"}";
url = URLEncoder.encode(url, "UTF-8"); 

From URLEncoder Class Documentation:

public static String encode(String s, String enc)

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.

Sign up to request clarification or add additional context in comments.

2 Comments

I would also recomment not sending the payload in an url parameter. The payload should be in the body so you can share the url with other people without them knowing what you send to the server.
@Yahya i have tried to encode the url before connecting to the url urlconnect. Still i am getting the same issue.Sysout shows the correct encoded url though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.