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.

Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84
CyprUS
  • 4,159
  • 9
  • 48
  • 93
  • I changed my URL to http://in.finance.yahoo.com/q?s=^NSEI . Still, it shows Error. So , i removed ^ from the query. It works, but i dont get the page i want. isnt ^ allowed in URL in java – CyprUS Mar 24 '11 at 06:53
  • For general solution have a look at http://stackoverflow.com/questions/749709/how-to-deal-with-the-urisyntaxexception/15570670#15570670 – Grigory Kislin Mar 22 '13 at 12:51
  • For a more general solution have a look at http://stackoverflow.com/a/20874602/205607 ;-) – smola Jan 02 '14 at 00:07

2 Answers2

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.

Favonius
  • 13,959
  • 3
  • 55
  • 95
0

I think you should URL-encode your string:

URI myNewLocation = new URI(URLEncoder.encode(downloadURL, "UTF-8"));
MarcoS
  • 13,386
  • 7
  • 42
  • 63