• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Parsing an HTML document. URL format problem

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm parsing HTML pages from a web site. I use this piece of code:

URL url = new URL(urlStr);
content = url.openStream();

'urlStr' is String type and I mount it before. I've discovered that I got and HTTP 400 error (malformed url exception I think) when I use spaces inside the URL, i.e:

http://www.yahoo.es?section=user mail&index=5

I've changed the code to change spaces by '%20':

urlStr = StringUtils.replace(urlStr," ","%20");
URL url = new URL(urlStr);
content = url.openStream();

and now works fine. My question is if there's a way (method, class...) that could validate URL formats and change invalid chars like spaces (in this case) to understandable chars and format to do the 'url.openStream()' in any case.

Thanks in advance.
 
Marshal
Posts: 28293
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. The class java.net.URLEncoder exists to do precisely that. Remember to encode only the part of the URL after the ? character.
 
Daniel Cote
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks for your help
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic