码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-01-08 18:01:13      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:http协议   服务器   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 = request.getHeader(name);
		System.out.println(name + "=" + value);
	}
	System.out.println("--------request.getHeader() end--------");
	// 使用getParameter方法得到请求发来的数据
	String personjsonstr = request.getParameter("personjson");
	String zhengzhuangstr = request.getParameter("zhengzhuangjson");
	System.out.println(personjsonstr);
	System.out.println(zhengzhuangstr);

输出结果:
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协议有个好的理解,是学习发送请求、传输数据到服务器的基础。

参考链接:

HTTP协议教程:



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

标签:http协议   服务器   post请求   

原文地址:http://blog.csdn.net/xingjiyuan26/article/details/42526391

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!