The http post method connects to the server, sends data to the server, and obtains data from the server. The post Server
The general process is: the client fills in the user name and password and makes a judgment on the server. If the verification password is correct, the logon is successful. If the password is incorrect, the logon failure is returned.
The client is a java program with the following code:
Package lgx. java. test; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. io. unsupportedEncodingException; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; import java.net. URLEncoder; import java. util. hashMap; import java. util. map; public class HttpPostDemo {private static String PA TH = "http: // 192.168.1.48: 8080/myapp2/LoginAction"; private static URL url; static {try {url = new URL (PATH);} catch (MalformedURLException e) {e. printStackTrace () ;}/ *** @ param args */public static void main (String [] args) {Map <String, String> param = new HashMap <String, string> (); param. put ("username", "admin"); param. put ("password", "123"); String result = sendPostMessage (param, "UTF-8"); System. out. Println ("result is" + result);} private static String sendPostMessage (Map <String, String> param, String encode) {StringBuffer buffer = new StringBuffer (); if (param! = Null &&! Param. isEmpty () {for (Map. entry <String, String> entry: param. entrySet () {try {buffer. append (entry. getKey ()). append ("= "). append (URLEncoder. encode (entry. getValue (), encode )). append ("&");} catch (UnsupportedEncodingException e) {e. printStackTrace () ;}try {HttpURLConnection connection = (HttpURLConnection) url. openConnection (); connection. setRequestMethod ("POST"); connection. setDoInput (true); connectio N. setDoOutput (true); byte [] data = buffer. toString (). getBytes (); // set the request type to text, and the string in it is the default connection. setRequestProperty ("Content-Tyte", "application/x-www-form-urlencoded"); connection. setRequestProperty ("Content-Length", String. valueOf (data. length); // output stream, which sends data to the server OutputStream outputStream = connection. getOutputStream (); outputStream. write (data); int requestCode = connection. getResponseCode (); if (requ EstCode = 200) {// input stream, get the server data return StreamToString (connection. getInputStream (), encode) ;}} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} return "";} /*** convert an input stream to a specified encoded String ** @ param inputStream input stream * @ param encode encoding format * @ return String */private static String StreamToString (InputStream inputStream, string encode) {ByteArrayOutputStream arrayOutputStream = new ByteA RrayOutputStream (); byte [] data = new byte [1024]; String result = ""; int len = 0; try {while (len = inputStream. read (data ))! =-1) {arrayOutputStream. write (data, 0, len);} result = new String (arrayOutputStream. toByteArray (), encode);} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return result ;}}
The server is a tomcat server. In the project, you have created a servlet. The code in the servlet is as follows:
package com.login.manager;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class LoginAction */public class LoginAction extends HttpServlet {private static final long serialVersionUID = 1L; /** * Default constructor. */ public LoginAction() { // TODO Auto-generated constructor stub }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubthis.doPost(request, response);}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");PrintWriter out=response.getWriter();String username=request.getParameter("username");String password=request.getParameter("password");System.out.println(username+"---->username");System.out.println(password+"----->password");if(username.equals("admin")&&password.equals("123")){out.print("longin is success");}else{out.print("login is failed");}}}
Use these two codes to enable the server first and then run the client. If both the user name and password are correct,
The console of the client is:
The result is longin is success.
The console of the server is:
Admin ----> username
123 -----> password