首页 > 代码库 > java客户端数据发送到服务器(POST请求)总结

java客户端数据发送到服务器(POST请求)总结

今天为了测试服务处理请求的功能,自己学了从客户端发送模拟浏览器发送请求,现在总结如下:
首先看写的相关的代码

客户端:

客户端主要用到的类是URLConnection

	URL url = new URL("http://localhost:8080/yiliaotest/RetransServlet");
	URLConnection con = url.openConnection();
	// post请求必须设置下面两项
	con.setDoOutput(true);
	con.setDoInput(true);
	// 不使用缓存
	con.setUseCaches(false);
	String personjson = "[{\"xingming\":\"namezxc\",\"xingbie\":0}]";
	String zhengzhuangjson = "[{\"kesou\":0}]";

	// 设置自定义的请求头,也可以用这个方法得到发送数据
	con.setRequestProperty("personjson", personjson);
	con.setRequestProperty("zhengzhuangjson", zhengzhuangjson);
	// 这句是打开链接
	OutputStream out = con.getOutputStream();
	// 把数据写到报文
	out.write(("zhengzhuangjson=" + zhengzhuangjson).getBytes());
	// &号是数据间隔,
	out.write("&".getBytes());
	out.write(("personjson=" + personjson).getBytes());

	out.flush();
	out.close();
	// 这句才是真正发送请求
	con.getInputStream();

服务器端
在doPost方法里写语句
	System.out.println("getContentType: " + request.getContentType());
	System.out.println("getQueryString: " + request.getQueryString());
	System.out.println("getRemoteAddr: " + request.getRemoteAddr());
	System.out.println("getRemoteHost: " + request.getRemoteHost());
	System.out.println("getRemotePort: " + request.getRemotePort());
	System.out.println("getRemoteUser: " + request.getRemoteUser());
	System.out.println("getLocalAddr: " + request.getLocalAddr());
	System.out.println("getLocalName: " + request.getLocalName());
	System.out.println("getLocalPort: " + request.getLocalPort());
	System.out.println("getMethod: " + request.getMethod());
	System.out.println("-------request.getParamterMap()-------");
	// 得到请求的参数Map,注意map的value是String数组类型
	Map map = request.getParameterMap();
	Set<String> keySet = map.keySet();
	for (String key : keySet) {
		String[] values = (String[]) map.get(key);
		for (String value : values) {
			System.out.println(key);
			System.out.println(key + "=" + value);
		}
	}
	System.out.println("-------request.getParamterMap() end-------");
	System.out.println("--------request.getHeader()--------");
	// 得到请求头的name集合
	Enumeration<String> em = request.getHeaderNames();
	while (em.hasMoreElements()) {
		String name = (String) em.nextElement();
		String value = http://www.mamicode.com/request.getHeader(name);>

输出结果:
getContentType: application/x-www-form-urlencoded
getQueryString: null
getRemoteAddr: 127.0.0.1
getRemoteHost: 127.0.0.1
getRemotePort: 59236
getRemoteUser: null
getLocalAddr: 127.0.0.1
getLocalName: localhost
getLocalPort: 8080
getMethod: POST
-------request.getParamterMap()-------
zhengzhuangjson
zhengzhuangjson=[{"kesou":0}]
personjson
personjson=[{"xingming":"namezxc","xingbie":0}]
-------request.getParamterMap() end-------
--------request.getHeader()--------
personjson=[{"xingming":"namezxc","xingbie":0}]
zhengzhuangjson=[{"kesou":0}]
cache-control=no-cache
pragma=no-cache
user-agent=Java/1.6.0_65
host=localhost:8080
accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
connection=keep-alive
content-type=application/x-www-form-urlencoded
content-length=77
--------request.getHeader() end--------
[{"xingming":"namezxc","xingbie":0}]
[{"kesou":0}]

总结:
1.如果不设置Content-type,默认是:application/x-www-form-urlencoded。
2.GET请求的参数与对应的值位于请求行中,并附加在URL后面,通过“?”分隔开来。
POST请求的数据在请求报文里。
3.两种请求方式的数据都以“key1=value1&key2=value”的格式。
4.Post用URLConnection里的OutputStream对象写入数据,服务端用HttpServletRequest的getParameter(key)方法得到属性值value。
5.对http协议有个好的理解,是学习发送请求、传输数据到服务器的基础。

参考链接:
关于Get与Post请求的区别:
http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html

HTTP协议教程:
http://www.w3cschool.cc/http/http-tutorial.html



java客户端数据发送到服务器(POST请求)总结