today in order to test the functionality of the service processing request, I learned to send the request from the client to send the simulation browser, and now summarizes the following:first look at the relevant code to write
Client:
The main class used by clients is URLConnection
URL url = new URL ("Http://localhost:8080/yiliaotest/RetransServlet"); URLConnection con = url.openconnection ();//Post request must have the following two entries set Con.setdooutput (true); Con.setdoinput (true);// Do not use cache con.setusecaches (false); String Personjson = "[{\" xingming\ ": \" namezxc\ ", \" Xingbie\ ": 0}]"; String Zhengzhuangjson = "[{\" kesou\ ": 0}]";//Set a custom request header, or you can use this method to get the data sent Con.setrequestproperty ("Personjson", Personjson) Con.setrequestproperty ("Zhengzhuangjson", Zhengzhuangjson);//This sentence is open link outputstream out = Con.getoutputstream ();//write the data to the message Out.write (("zhengzhuangjson=" + Zhengzhuangjson). GetBytes ());//& is the data interval, Out.write ("&". GetBytes ()); Out.write (("personjson=" + Personjson). GetBytes ()); Out.flush (); Out.close ();// This sentence is really send request con.getinputstream ();
Server-sidewrite a statement in the Dopost method
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 ()-------");//Get the requested parameter map, note that the value of map is a string array type map map = Request.getparametermap (); set<string> KeySet = Map.keyset (); for (string key:keyset) {string[] values = (string[]) map.get (key), for (String V Alue:values) {System.out.println (key); SYSTEM.OUT.PRINTLN (key + "=" + value);}} SYSTEM.OUT.PRINTLN ("-------request.Getparamtermap () end-------"); System.out.println ("--------Request.getheader ()--------");//Gets the name collection of the request header 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--------");//Use the GetParameter method to get the data sent by the request string personjsonstr = Request.getparameter ("Personjson"); String zhengzhuangstr = Request.getparameter ("Zhengzhuangjson"); System.out.println (PERSONJSONSTR); System.out.println (ZHENGZHUANGSTR);
Output Result:
Getcontenttype:application/x-www-form-urlencodedgetquerystring:nullgetremoteaddr:127.0.0.1getremotehost: 127.0.0.1getremoteport:59236getremoteuser:nullgetlocaladdr:127.0.0.1getlocalname:localhostgetlocalport: 8080getmethod:post-------Request.getparamtermap ()-------zhengzhuangjsonzhengzhuangjson=[{"Kesou": 0}] personjsonpersonjson=[{"xingming": "Namezxc", "Xingbie": 0}]-------Request.getparamtermap () End---------------Request.getheader ()--------personjson=[{"xingming": "Namezxc", "Xingbie": 0}]zhengzhuangjson=[{ "Kesou": 0}]cache-control=no-cachepragma=no-cacheuser-agent=java/1.6.0_65host=localhost:8080accept=text/html, Image/gif, Image/jpeg, *; Q=.2, */*; q=.2connection=keep-alivecontent-type=application/x-www-form-urlencodedcontent-length=77-------- Request.getheader () end--------[{"xingming": "Namezxc", "Xingbie": 0}][{"Kesou": 0}]
Summary: 1. If Content-type is not set, the default is:application/x-www-form-urlencoded. The parameters of the 2.GET request are located in the request line, appended to the URL, through the "? "Separated. the data for the post request is in the request message. 3. The data for the two request methods are in "Key1=value1&key2=value" format. 4.Post writes data using the OutputStream object in the URLConnection , and the server uses httpservletrequest The GetParameter (key) method gets the property value. 5. A good understanding of the HTTP protocol is the basis for learning to send requests and transfer data to the server.
Reference Links:about the difference between get and POST requests:http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html
HTTP protocol Tutorial:http://www.w3cschool.cc/http/http-tutorial.html
Java client data is sent to the server (POST request) Summary