1

I am using the following code to show NSE indices in default browser.

String downloadURL = "http://in.finance.yahoo.com/q;_ylt=AkieA" +
   "_4_rXXRBh2SH7_U3kXyULlG;_ylu=X3oDMTE1Nmc5cjBnBHBvcwMyBHNlY" +
   "wNmb290ZXIteWZpbmFuY2UEc2xrA25pZnR5NTA-?s=^NSEI";
java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
java.net.URI myNewLocation = new java.net.URI(downloadURL);
myNewBrowserDesktop.browse(myNewLocation);

The URL in mention has been copied from yahoo site itself. However, when i run the code , i get error like:

java.net.URISyntaxException: Illegal character in query at index 140: http://in.finance.yahoo.com/q;_ylt=AkieA_4_rXXRBh2SH7_U3kXyULlG;_ylu=X3oDMTE1Nmc5cjBnBHBvcwMyBHNlYwNmb290ZXIteWZpbmFuY2UEc2xrA25pZnR5NTA-?s=^NSEI

I dont know what is wrong ; i have done the same with other URLs with success. Please help.

3

2 Answers 2

5

As per this link:

http://www.blooberry.com/indexdot/html/topics/urlencoding.htm the Caret ("^") comes under unsafe category.

"Unsafe characters" Why: Some characters present the possibility of being misunderstood within URLs for various reasons. These characters should also always be encoded.

After changing the ^ to %5E it works.

Now if you copy the url from your browser and put it in another browser then it will work. I think the browser internally takes care of the special characters. But in java the java.net.URL is supposed to work with standalone and console applications and as well on different platforms, therefore, it is the responsibility of the developer to take care of encoding special characters.

Google for java based URL encoders.

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

Comments

0

I think you should URL-encode your string:

URI myNewLocation = new URI(URLEncoder.encode(downloadURL, "UTF-8"));

Comments

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.