JAVA uses the original HttpURLConnection to send POST data, httpurlconnection

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.