JavaJack
JavaJack

Reputation: 41

URISyntax Exception after encoding the URL

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.

Upvotes: 1

Views: 362

Answers (1)

Yahya
Yahya

Reputation: 14082

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.

Upvotes: 2

Related Questions