黑马程序员 java IO URL URLConnection


---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------


package itcast.video.network;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class I_06_URL_Demo {


/**
* @param args
* @throws MalformedURLException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// URL url = new
// URL("http://127.0.0.1:11000/web/index.html?name=username");
URL url = new URL("http://www.baidu.com");
methodURL(url);
methodURLConnection(url);


}


public static void methodURLConnection(URL url) throws IOException {
URLConnection conn = url.openConnection();
// sun.net.www.protocol.http.HttpURLConnection:
// http://127.0.0.1:11000/web/index.html?name=username
System.out.println(conn);
BufferedReader bf = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
//PrintWriter pw = new PrintWriter(conn.getOutputStream(), true);
String line = null;
while ((line = bf.readLine()) != null) {
//pw.println(line);
System.out.println(line);
}


}


public static void methodURL(URL url) {
// getProtocol: http
// getHost : 127.0.0.1
// getPort : 11000
// getPath : /web/index.html
// getFile : /web/index.html?name=username
// getQuery : name=username
System.out.println("getProtocol: " + url.getProtocol());
System.out.println("getHost    : " + url.getHost());
System.out.println("getPort    : " + url.getPort());
System.out.println("getPath    : " + url.getPath());
System.out.println("getFile    : " + url.getFile());
System.out.println("getQuery   : " + url.getQuery());


}


}



---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

你可能感兴趣的:(黑马程序员 java IO URL URLConnection)