一、看一个简单的浏览器服务端的例子
需求:服务端向浏览器发送一串字符,浏览器接收到。
服务端程序
import java.io.*;
import java.net.*;
public class ServerDemo {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(11001);
Socket s = ss.accept();
String ip =s.getInetAddress().getHostAddress();
System.out.println("ip" + ip + "...connected");
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println("我有一头小毛驴,我从来也不骑");
pw.flush();
s.close();
ss.close();
}
}
当在浏览器中输入http://192.168.1.103:11001/,可以看到浏览器显示服务端发送来的数据。
再看一下升级版,当浏览器向服务端发出请求时,究竟发送了什么?
重写服务端
import java.io.*;
import java.net.*;
public class ServerDemo1 {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(11001);
Socket s = ss.accept();
String ip =s.getInetAddress().getHostAddress();
System.out.println("ip" + ip + "...connected");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int length = in.read(buf);
System.out.println(new String(buf,0,length));
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println("我有一头小毛驴,我从来也不骑");
pw.flush();
s.close();
ss.close();
}
}
可以看一下输出结果
GET / HTTP/1.1
Host: 192.168.1.103:11001
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
以上便是http的请求消息头。
http协议目前只有两个版本,一个是1.0,一个是1.1
二、URL
示例
import java.net.*;
public class URLDemo {
public static void main(String[] args) throws Exception{
URL url = new URL("http://localhost:8080/webProject/test.html");
System.out.println("getFile():"+url.getFile());
System.out.println("getHost():"+url.getHost());
System.out.println("getPath():"+url.getPath());
System.out.println("getPort():"+url.getPort());
System.out.println("getProtocol():"+url.getProtocol());
System.out.println("getQuery():"+url.getQuery());
}
}
结果
getFile():/webProject/test.html
getHost():localhost
getPath():/webProject/test.html
getPort():8080
getProtocol():http
getQuery():null
可以看到getFile和getPath获取的信息相同,这是在getQuery没有的情况下。
再来看令一种url:
import java.net.*;
public class URLDemo {
public static void main(String[] args) throws Exception{
URL url = new URL("http://localhost:8080/webProject/test.html?name=haha&age=20");
System.out.println("getFile():"+url.getFile());
System.out.println("getHost():"+url.getHost());
System.out.println("getPath():"+url.getPath());
System.out.println("getPort():"+url.getPort());
System.out.println("getProtocol():"+url.getProtocol());
System.out.println("getQuery():"+url.getQuery());
}
}
getFile():/webProject/test.html?name=haha&age=20
getHost():localhost
getPath():/webProject/test.html
getPort():8080
getProtocol():http
getQuery():name=haha&age=20
当getQuery不为空的情况下,getFile和getPath是不一样的。
三、URLConnection
需求:客户端向tomcat服务器发出请求,客户端打印服务端发送来的请求。
import java.net.*;
import java.io.*;
public class URLConnectionDemo {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
URL url = new URL("http://localhost:8080/webProject/test.html?name=haha&age=20");
URLConnection con = url.openConnection();
InputStream in =con.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
}
}
响应结果
<html>
<body>
<h1>这是我的主页<h1>
<font size = 5 color = red> 静夜思 </font>
<div center>
窗前明月光<br>
疑是地上霜<br>
举头望明月<br>
低头思故乡<br>
<div>
</body>
</html>