Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2004-11-05
23
Medium Priority
?
197 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:kumvjuec
  • 11
  • 8
  • 3
  • +1
23 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12505667
String encodedURL=java.net.URLEncoder.encodeUrl(urURL);
then open ur URL connection
0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12505700
This is giving exception MalFormedUrlException. It even encodes the colons etc.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12505728
http://www.javaalmanac.com/egs/java.net/Post.html

Also look at the "Related Examples".
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 12505786
Please post the url you want to encode
0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12505929
http://www.site.com/(Java's)Table of contents.pdf
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12505964
OK - but i don't see the aforementioned colons in there:

>>It even encodes the colons etc.
0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12505997
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12506013
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 12512341
Hey CEHJ, how're things ;-) ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12512549
Not bad thanks - plugging away! And you?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12512668
Tough :-( and hectic, as you can see.... haven't been able to do much EE since months :(
0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12512693
Seems like I have again united two long lost friends ;-)
May be I should keep the question open for some more time :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12512717
LOL - but what is your position on the question?
0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12512741
This is not working. It is encoding "/" also.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12512744
No - you shouldn't be including any '/'
0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12512752
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 12512812
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
 
LVL 3

Author Comment

by:kumvjuec
ID: 12512960
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12512975
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12512995
>>but is it for sure that it is only the "/" encoding that creates problem

You could probably say, on the whole, yes
0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12513061
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12513075
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12518528
8-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question