java客户端数据发送到服务器(POST请求)总结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java客户端数据发送到服务器(POST请求)总结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3883字,纯文字阅读大概需要6分钟。
内容图文
![java客户端数据发送到服务器(POST请求)总结](/upload/InfoBanner/zyjiaocheng/1314/d71c4cdfa8024d2d85543b6b3a1da326.jpg)
客户端:
客户端主要用到的类是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();
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}]
原文:http://blog.csdn.net/xingjiyuan26/article/details/42526391
内容总结
以上是互联网集市为您收集整理的java客户端数据发送到服务器(POST请求)总结全部内容,希望文章能够帮你解决java客户端数据发送到服务器(POST请求)总结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。