0

java.lang.IllegalArgumentException: Illegal character in query at index 49: http://download.finance.yahoo.com/d/quotes.csv?s=?^NDX,^BANK&f=nsl1opc1

im getting the malformed url exception on the 'caret' (^) symbol..came to know that the caret is used heavily in regex and in other cases this character is particularly removed from a desired string.

Is this not possible to have strings/urls with 'caret' (^) symbol

In the above case to retrieve index data i need to have that symbol..

x-code
  • 608
  • 1
  • 10
  • 30

1 Answers1

2

It is indeed not legal to have a caret in a query string part.

Use the constructor for URI instead:

final URL url = new URI("http", "download.finance.yahoo.com", "/d/quotes.csv",
    "s=?^NDX,^BANK&f=nsl1opc1", null).toURL();

This will take care of all the necessary encoding issues for you.

See the Javadoc.

fge
  • 119,121
  • 33
  • 254
  • 329
  • 1
    Well, there is no reason why it should not work. Try it and see ;) – fge May 26 '13 at 18:38
  • Side note: if you have many URLs to build that way, one project of mine may be useful: https://github.com/fge/uri-template – fge May 26 '13 at 19:04