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.

JavaJack
  • 41
  • 5
  • Can you please post your code to know how you have encoded and use it – Amit Bera Jun 02 '18 at 18:53
  • Probably a duplicate of https://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters – Daniele Jun 02 '18 at 18:57
  • You need to encode the string using `URLEncoder`, the error in the logs is complaining about the invalid character `{` – Daniele Jun 02 '18 at 18:59
  • 1
    Possible duplicate of [Java URL encoding of query string parameters](https://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters) – vinS Jun 02 '18 at 19:10
  • @Amit Bera i have encoded in the same way as in the answer by yahya. – JavaJack Jun 02 '18 at 21:26

1 Answers1

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.

Community
  • 1
  • 1
Yahya
  • 13,349
  • 6
  • 30
  • 42
  • 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. – modmoto Jun 02 '18 at 19:34
  • @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. – JavaJack Jun 02 '18 at 21:24