About Java using raw HttpURLConnection to send post data _java

Source: Internet
Author: User
Tags readline stringbuffer

URLConnection is an abstract class that has two direct subclasses, respectively, HttpURLConnection and Jarurlconnection. Another important class is the URL, which typically generates a URL instance to a specific address by passing the constructor a string parameter.

Each httpurlconnection instance can be used to generate a single request, but other instances can transparently share the underlying network that connects to the HTTP server. After the request, the close () method is raised in the HttpURLConnection InputStream or OutputStream to release the network resources associated with this instance, but has no effect on the shared persistent connection. If a persistent connection is idle when disconnect () is invoked, the underlying socket may be turned off.

Package Com.newflypig.demo;
/** * Use JDK's own httpurlconnection to send post requests to URLs and output response results * Parameters are routed using streaming and hard-coded into 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 ("HT 
    Tp://localhost:8080/test1/helloworld ");
    String urlpath = new String ("Http://localhost:8080/Test1/HelloWorld?name= Tintin". GetBytes ("UTF-8"));
    String param= "Name=" +urlencoder.encode ("Ding Ding", "UTF-8");
    Establish the connection URL url=new url (urlpath);
    HttpURLConnection httpconn= (httpurlconnection) url.openconnection ();   Sets the parameter Httpconn.setdooutput (TRUE);   Output Httpconn.setdoinput (TRUE) is required;  You need to enter httpconn.setusecaches (false);   Caching Httpconn.setrequestmethod ("POST") is not allowed; Set post mode 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");
    Connection, you can use the following Httpconn.getoutputstream () automatically connect httpconn.connect ();
    Creates an input stream and passes the parameter DataOutputStream dos=new dataoutputstream (Httpconn.getoutputstream ()) to the URL to point to.
    Dos.writebytes (param);
    Dos.flush ();
    Dos.close ();
    Get 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 a outputstream stream

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

The above content is this article all said, hoped this article introduction is helpful to everybody.

Related Article

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.