package URLConnection;
import java.net.*;
import java.util.*;
/*
* 查看头部信息
*/
public class ConnTCP {
public static void main(String[] args) {
try {
URL u = new URL("http://localhost:8086/restrant/ch01/show.html");
URLConnection uc = u.openConnection();
Map m = uc.getHeaderFields();
Set set = m.keySet();
for (Object o : set) {
System.out.println(o + ":" + m.get(o));
}
System.out.println("/n");
// 利用迭代的方法查看头部信息
Iterator it = set.iterator();
while (it.hasNext()) {
Object s = it.next();
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package URLConnection;
import java.io.*;
import java.net.*;
public class test1 {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/");
URLConnection uc = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(uc
.getInputStream()));
String line;
if ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.fillInStackTrace();
}
}
}