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
LVL 3
kumvjuecAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
String encodedURL=java.net.URLEncoder.encodeUrl(urURL);
then open ur URL connection
0
kumvjuecAuthor Commented:
This is giving exception MalFormedUrlException. It even encodes the colons etc.
0
Mayank SAssociate Director - Product EngineeringCommented:
http://www.javaalmanac.com/egs/java.net/Post.html

Also look at the "Related Examples".
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

CEHJCommented:
Please post the url you want to encode
0
kumvjuecAuthor Commented:
http://www.site.com/(Java's)Table of contents.pdf
0
CEHJCommented:
OK - but i don't see the aforementioned colons in there:

>>It even encodes the colons etc.
0
kumvjuecAuthor Commented:
I was talking about URLEncoder.encode function. It will encode almost everything to give http%**%**%**www*******
Then I cannot make URL out of it.
0
CEHJCommented:
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);
0
Mayank SAssociate Director - Product EngineeringCommented:
Hey CEHJ, how're things ;-) ?
0
CEHJCommented:
Not bad thanks - plugging away! And you?
0
Mayank SAssociate Director - Product EngineeringCommented:
Tough :-( and hectic, as you can see.... haven't been able to do much EE since months :(
0
kumvjuecAuthor Commented:
Seems like I have again united two long lost friends ;-)
May be I should keep the question open for some more time :)
0
CEHJCommented:
LOL - but what is your position on the question?
0
kumvjuecAuthor Commented:
This is not working. It is encoding "/" also.
0
CEHJCommented:
No - you shouldn't be including any '/'
0
kumvjuecAuthor Commented:
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
0
CEHJCommented:
Try something like:


final String protocol = "http";
String host = "www.site.com";
String filePart = "/pdfs/mcgraw's new edition/(Java's)Table of contents.pdf";
StringBuffer sb = new StringBuffer(filePart.length() * 3);
StringTokenizer st = new StringTokenizer(filePart, "/", true);
while (st.hasMoreTokens()) {
      String token = st.nextToken();
      if (!token.equals("/")) {
      token = URLEncoder.encode(token);
      }
      sb.append(token);
}
URL url = new URL(protocol, host, sb.toString());
System.out.println(url);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kumvjuecAuthor Commented:
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.
0
CEHJCommented:
>>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 ..?
0
CEHJCommented:
>>but is it for sure that it is only the "/" encoding that creates problem

You could probably say, on the whole, yes
0
kumvjuecAuthor Commented:
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.
0
CEHJCommented:
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
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.