自定义web浏览器客户端:
我们自定义一个注册表单页面:
源代码:
<form action="http://192.168.43.237:9090" method="post">
<table border="2" bordercolor="red" width="500" cellpadding="10" cellspacing="10">
<tr>
<th colspan="2">注册表单</th>
</tr>
<tr>
<td>用户名称:</td>
<td>
<input type="text" name="user"/>
</td>
</tr>
<tr>
<td>输入密码:</td>
<td>
<input type="password" name="psw"/>
</td>
</tr>
<tr>
<td>确认密码:</td>
<td>
<input type="password" name="repsw"/>
</td>
</tr>
<tr>
<td>选择性别:</td>
<td>
<input type="radio" name="sex" checked="checked" value="man"/>男
<input type="radio" name="sex" value="woman"/>女
</td>
</tr>
<tr>
<td>选择爱好:</td>
<td>
<input type="checkbox" name="hobby" value="java"/>Java
<input type="checkbox" name="hobby" value="html"/>HTML
<input type="checkbox" name="hobby" value="css"/>CSS
<input type="checkbox" name="hobby" value="dom"/>DOM
</td>
</tr>
<tr>
<td>选择国家:</td>
<td>
<select name="country">
<option value="none">--选择国家--</option>
<option value="cn" selected="selected">--中国--</option>
<option value="usa">--美国--</option>
<option value="en">--英国--</option>
</select>
</td>
</tr>
<tr>
<th colspan="2">
<input type="reset" value="重置"/>
<input type="submit" value="提交">
</th>
</tr>
</table>
</form>
<hr/>
自定义本地服务器:
上述表单提交的ip地址,我是本地的ip地址:192.168.43.237
服务器会将客户端发来的消息打印出来,并向客户端发送“注册成功”信息。
import java.io.*;
import java.net.*;
class WebServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(9090);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress());
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
PrintWriter out = new PrintWriter(s.getOutputStream());
out.println("<font color='green'>注册成功!</font>");
s.close();
ss.close();
}
}
操作流程:
启动服务器:
客户端使用get方式,向服务器提交表单:
服务器获取的数据:
客户端会显示:注册成功
总结:
从上述流程可以看出,其实浏览器向客户端发送求时,不管是使用地址栏,表单还是超链接的方式,其实都是使用Socket 对象,将上述信息使用Socket对象的OutputStream输出流将数据字符一行行发送给服务器。
而服务器可以使用链接上的Socket对象的InputStream输入流,来获取发送过来的信息,并进行处理。
所以我们可以自己写一个IE浏览器的java程序:
自定义命令行界面的IE浏览器:
import java.io.*;
import java.net.*;
class MyIE
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.43.237",9090);
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("GET /?user=xjy&psw=123&repsw=123&sex=man&hobby=java&hobby=html&country=usa HTTP/1.1");
out.println("Accept: */*");
//out.println("");
out.println();
out.println();
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while((line=bufr.readLine())!=null)
{
System.out.println(line);
}
s.close();
}
}
客户端:
服务器:
浏览器get和post两种提交方式的区别:
1:get提交方式
地址栏:http://192.168.43.237:9090/? user=xjy&psw=123&repsw=123&sex=man&hobby=java&hobby=html&country=cn
向服务器提交的数据信息:
GET /?user=xjy&psw=123&repsw=123&sex=man&hobby=java&hobby=html&country=cn HTTP/1
.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xm
l, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; rv:11.0) like
Gecko
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host: 192.168.43.237:9090
Connection: Keep-Alive
2:post提交方式
地址栏:http://192.168.43.237:9090/
向服务器提交的数据信息:
POST / HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xm
l, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; rv:11.0) like
Gecko
Content-Type: application/x-www-form-urlencoded
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host: 192.168.43.237:9090
Content-Length: 70
Connection: Keep-Alive
Cache-Control: no-cache
user=xjy&psw=123&repsw=123&sex=woman&hobby=java&hobby=html&country=usa
3:get提交方式和post提交方式的区别:
1.
get提交,提交的信息都显示在地址栏中
post提交,提交信息不显示在地址栏中
2.
get提交,对于敏感数据不安全
post提交,数据安全
3.
get提交,对于大数据不行,因为地址栏存储体积有限
post提交,可提交大体积数据
4.
get提交,将信息封装到请求信息的请求行中
post提交,将信息封装到了请求体中