URLConnection is an abstract class, which has two direct subclasses: HttpURLConnection and JarURLConnection. Another important class is URL. Usually URL can generate a URL instance pointing to a specific address by passing a String type parameter to the constructor.
Each HttpURLConnection instance can be used to make a single request, but other instances can transparently share the underlying network connection to the HTTP server. Calling the close() method on the InputStream or OutputStream of the HttpURLConnection after the request releases the network resources associated with this instance, but has no effect on the shared persistent connection. If the persistent connection is idle when disconnect() is called, the underlying socket may be closed.
package com.newflypig.demo;/** * Use the HttpURLConnection that comes with jdk to send a POST request to the URL and output the response result * Parameters are passed using streams and are hard-coded into the format of the string "name=XXX" */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=DingDing".getBytes("UTF-8")); String param="name="+URLEncoder.encode("DingDing","UTF-8 "); //Establish connection URL url=new URL(urlPath); HttpURLConnection httpConn=(HttpURLConnection)url.openConnection(); //Set parameters httpConn.setDoOutput(true); //Need to output httpConn.setDoInput(true); //Need to input httpConn.setUseCaches(false); //Do not allow caching httpConn.setRequestMethod("POST"); //Set POST connection //Set request properties httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpConn.setRequestProperty("Connection", "Keep-Alive");//Maintain a long connection httpConn.setRequestProperty("Charset", "UTF-8"); //Connect, you can also use the following httpConn without clear text connect. getOutputStream() will automatically connect httpConn.connect(); //Establish an input stream and pass the parameter DataOutputStream dos=new to the URL pointed to. DataOutputStream(httpConn.getOutputStream()); dos.writeBytes(param); dos.flush(); dos.close(); //Get the response status int resultCode=httpConn.getResponseCode(); if(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 in the form of OutputStream stream.
During the specific encoding process, the parameters are sent in the form of the string "name=XXX"
The above content is all described in this article. I hope this introduction will be helpful to everyone.