Link to home
Start Free TrialLog in
Avatar of kumvjuec
kumvjuec

asked on

URL encoding - Why I am not able to make URL object?

hi,

I want to encode an arbitrary URL so that I can escape certain characters and then make a URL object and make a connection. Can you suggest how to go about doing this. Thanks
Avatar of petmagdy
petmagdy
Flag of Canada image

String encodedURL=java.net.URLEncoder.encodeUrl(urURL);
then open ur URL connection
Avatar of kumvjuec
kumvjuec

ASKER

This is giving exception MalFormedUrlException. It even encodes the colons etc.
Avatar of Mayank S
http://www.javaalmanac.com/egs/java.net/Post.html

Also look at the "Related Examples".
Please post the url you want to encode
http://www.site.com/(Java's)Table of contents.pdf
OK - but i don't see the aforementioned colons in there:

>>It even encodes the colons etc.
I was talking about URLEncoder.encode function. It will encode almost everything to give http%**%**%**www*******
Then I cannot make URL out of it.
Oh , i see what you mean. Just encode the path part

String path =java.net.URLEncoder.encodeUrl("(Java's)Table of contents.pdf");
URL url = new URL("http://www.site.com/"  + path);
Hey CEHJ, how're things ;-) ?
Not bad thanks - plugging away! And you?
Tough :-( and hectic, as you can see.... haven't been able to do much EE since months :(
Seems like I have again united two long lost friends ;-)
May be I should keep the question open for some more time :)
LOL - but what is your position on the question?
This is not working. It is encoding "/" also.
No - you shouldn't be including any '/'
I have to work with lot of URLs, so I may even get anything like

http://www.site.com/pdfs/mcgraw's new edition/(Java's)Table of contents.pdf
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, this works, but is it for sure that it is only the "/" encoding that creates problem. There might be another character also. The urls can be of any protoccol - http, ftp, https, those used for streaming media also. I will have to do lot of work. It would be great if a simple method call could do the job.
>>but is it for sure that it is only the "/" encoding that creates problem

Difficult to answer that. The URLEncoder is normally used for small parts of a url.
What seems strange to me is that your 'urls' are so far from being usable in the first place. btw, in what form are they - in a file, in a List ..?
>>but is it for sure that it is only the "/" encoding that creates problem

You could probably say, on the whole, yes
They are in log files - which are being generated by another application. Here, the problem is that for readibility, the application is first decoding the URLs and then writing them to log file. I must handle all cases properly.
It'd probably give you less grief if you used two log files maybe. Otherwise use what i mentioned before, after discovering the protocol and host
8-)