java net 学习(二)--URL
new URL(“http”,”www.google.com.hk”,”/map.html”);
URL解析方法:
getProtocol
Returns theprotocol identifier component of the URL.
getAuthority
Returns theauthority component of the URL.
getHost
Returns the hostname component of the URL.
getPort
Returns the portnumber component of the URL. The getPort method returns an integer that is theport number. If the port is not set, getPort returns -1.
getPath
Returns the pathcomponent of this URL.
getQuery
Returns the querycomponent of this URL.
getFile
Returns thefilename component of the URL. The getFile method returns the same as getPath,plus the concatenation of the value of getQuery, if any.
getRef
Returns thereference component of the URL.
public class Reverse {
??? public staticvoid main(String[] args) throws Exception {
?
??????? if(args.length != 2) {
??????? ??? System.err.println("Usage:? java Reverse " +
??????????????????????????????"http://<location of your servlet/script>" +
???????????????? ??????????????" string_to_reverse");
??????? ??? System.exit(1);
??????? }
?
??????? StringstringToReverse = URLEncoder.encode(args[1], "UTF-8");
?
??????? URL url =new URL(args[0]);
??????? URLConnectionconnection = url.openConnection();
??????? connection.setDoOutput(true);
?
??????? OutputStreamWriterout = new OutputStreamWriter(
?????????????????????????????connection.getOutputStream());
??????? out.write("string="+ stringToReverse);
??????? out.close();
?
??????? BufferedReaderin = new BufferedReader(
?????????????????????????????? newInputStreamReader(
?????????????????????????????? connection.getInputStream()));
??????????????????????????????
??????? StringdecodedString;
?
??????? while((decodedString = in.readLine()) != null) {
??????? ??? System.out.println(decodedString);
??????? }
??????? in.close();
??? }
}