JAVA uses the original HttpURLConnection to send POST data, httpurlconnection
Package com. newflypig. demo;/*** use the HttpURLConnection provided by jdk to send a POST request to the URL and output the response result * the parameter is transmitted using a stream, and hard-coded as the string "name = XXX" format */import java. io. bufferedReader; import java. io. dataOutputStream; import java. io. inputStreamReader; import java.net. httpURLConnection; import java.net. URL; import java.net. URLEncoder; public class SendPostDemo {public static void main (String [] args) throws Exception {String urlPath = new String (" Http: // localhost: 8080/Test1/HelloWorld "); // String urlPath = new String (" http: // localhost: 8080/Test1/HelloWorld? Name = ding ". getBytes ("UTF-8"); String param = "name =" + URLEncoder. encode ("ding", "UTF-8"); // establish a connection URL url = new URL (urlPath); HttpURLConnection httpConn = (HttpURLConnection) url. openConnection (); // set the parameter httpConn. setDoOutput (true); // You Need to output httpConn. setDoInput (true); // you need to enter httpConn. setUseCaches (false); // httpConn cannot be cached. setRequestMethod ("POST"); // sets the POST method connection // sets the request attribute httpConn. setRequestProperty ("Content-Type", "application/x- Www-form-urlencoded "); httpConn. setRequestProperty ("Connection", "Keep-Alive"); // maintain the persistent Connection httpConn. setRequestProperty ("Charset", "UTF-8"); // connection, you can also use the following httpConn without plaintext connect. getOutputStream () automatically connects cthttpconn. connect (); // create an input stream and input the parameter DataOutputStream dos = new DataOutputStream (httpConn. getOutputStream (); dos. writeBytes (param); dos. flush (); dos. close (); // get the response status int resultCode = httpConn. getResponseCode (); I F (HttpURLConnection. HTTP_ OK = resultCode) {StringBuffer sb = new StringBuffer (); String readLine = new String (); BufferedReader responseReader = new BufferedReader (new InputStreamReader (httpConn. getInputStream (), "UTF-8"); while (readLine = responseReader. readLine ())! = Null) {sb. append (readLine ). append ("\ n");} responseReader. close (); System. out. println (sb. toString ());}}}
JAVA uses HttpURLConnection to send POST data by sending OutputStream streams.
The parameter is sent in the format of "name = XXX" in the specific encoding process.