客户端:
客户端主要用到的类是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