Talk about Java sending POST data using raw HttpURLConnection

  • 2020-04-01 04:17:09
  • OfStack

URLConnection is an abstract class with two direct subclasses: HttpURLConnection and JarURLConnection. Another important class is the URL, which typically generates a URL instance to a specific address by passing a string-type parameter to the constructor.

Each HttpURLConnection instance can be used to generate a single request, but other instances can transparently share the underlying network connected to the HTTP server. Calling the close() method on the InputStream or OutputStream of HttpURLConnection after the request frees up the network resources associated with this instance, but has no effect on the Shared persistent connection. If the persistent connection is idle when you call disconnect(), you might close the base socket.


package com.newflypig.demo;

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 & # 63; Name = tintin ". GetBytes (" utf-8 "));
    String param="name="+URLEncoder.encode(" tintin ","UTF-8");
    //Establish a connection
    URL url=new URL(urlPath);
    HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
    //Set the parameters
    httpConn.setDoOutput(true);   //Need to output
    httpConn.setDoInput(true);   //Need to enter
    httpConn.setUseCaches(false);  //Caching is not allowed
    httpConn.setRequestMethod("POST");   //Set up a POST connection
    //Set request properties
    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    httpConn.setRequestProperty("Connection", "Keep-Alive");//Maintain long connection
    httpConn.setRequestProperty("Charset", "UTF-8");
    //Instead of plaintext connect, use the following httpConn. GetOutputStream () to automatically connect
    httpConn.connect();
    //Creates an input stream, passing in parameters to the URL to be pointed to
    DataOutputStream dos=new DataOutputStream(httpConn.getOutputStream());
    dos.writeBytes(param);
    dos.flush();
    dos.close();
    //Get response state
    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 as an OutputStream

In the specific encoding process, the parameter is sent in the form of the string "name=XXX"

The above is all of the content of this article, I hope this article is helpful to you.


Related articles: