Java Reference
In-Depth Information
Using this class, we can now encode the previous example:
QueryString qs = new QueryString ();
qs . add ( "hl" , "en" );
qs . add ( "as_q" , "Java" );
qs . add ( "as_epq" , "I/O" );
String url = "http://www.google.com/search?" + qs ;
System . out . println ( url );
URLDecoder
The corresponding URLDecoder class has a static decode() method that decodes strings
encoded in x-www-form-url-encoded format. That is, it converts all plus signs to spaces
and all percent escapes to their corresponding character:
public static String decode ( String s , String encoding )
throws UnsupportedEncodingException
If you have any doubt about which encoding to use, pick UTF-8. It's more likely to be
correct than anything else.
An IllegalArgumentException should be thrown if the string contains a percent sign
that isn't followed by two hexadecimal digits or decodes into an illegal sequence.
Since URLDecoder does not touch non-escaped characters, you can pass an entire URL
to it rather than splitting it into pieces first. For example:
String input = "https://www.google.com/" +
"search?hl=en&as_q=Java&as_epq=I%2FO" ;
String output = URLDecoder . decode ( input , "UTF-8" );
System . out . println ( output );
Proxies
Many systems access the Web and sometimes other non-HTTP parts of the Internet
through proxy servers . A proxy server receives a request for a remote server from a local
client. The proxy server makes the request to the remote server and forwards the result
back to the local client. Sometimes this is done for security reasons, such as to prevent
remote hosts from learning private details about the local network configuration. Other
times it's done to prevent users from accessing forbidden sites by filtering outgoing
requests and limiting which sites can be viewed. For instance, an elementary school
might want to block access to http://www.playboy.com . And still other times it's done
purely for performance, to allow multiple users to retrieve the same popular documents
from a local cache rather than making repeated downloads from the remote server.
Java programs based on the URL class can work through most common proxy servers
and protocols. Indeed, this is one reason you might want to choose to use the URL class
rather than rolling your own HTTP or other client on top of raw sockets.
Search WWH ::




Custom Search