Example of using HttpClient to send a Post request in jsp

Source: Internet
Author: User
Tags flush php and

The company wants to encapsulate its own product into a WebService platform, so it recently began to learn how to use Java to send Http request content. This section was previously written in PHP and used in most basic sockets and third-party plug-ins.

I learned two Java methods: java.net. URLConnection and HttpClient. Without in-depth research on efficiency, it is troublesome to use java.net. URLConnection, while HttpClient is more comfortable.

The code is as follows: Copy code

Java.net. URLConnection method:

Private static void urlConnectionPost (){
StringBuilder responseBuilder = null;
BufferedReader reader = null;
OutputStreamWriter wr = null;

URL url;
Try {
Url = new URL (TEST_URL );
URLConnection conn = url. openConnection ();
Conn. setDoOutput (true );
Conn. setConnectTimeout (1000*5 );
Wr = new OutputStreamWriter (conn. getOutputStream ());
Wr. write ("");
Wr. flush ();

// Get the response
Reader = new BufferedReader (new InputStreamReader (conn
. GetInputStream ()));
ResponseBuilder = new StringBuilder ();
String line = null;
While (line = reader. readLine ())! = Null ){
ResponseBuilder. append (line + "n ");
        }
Wr. close ();
Reader. close ();

System. out. println (responseBuilder. toString ());
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
    }

}


HttpClient method:

Private static void httpClientPost (){
HttpClient client = new DefaultHttpClient ();
HttpPost post = new HttpPost (TEST_URL );

Try {
ContentProducer cp = new ContentProducer (){
Public void writeTo (OutputStream outstream) throws IOException {
Writer writer = new OutputStreamWriter (outstream, "UTF-8 ");
Writer. write ("");
Writer. flush ();
            }
};

Post. setEntity (new EntityTemplate (cp ));
HttpResponse response = client.exe cute (post );
   
System. out. println (EntityUtils. toString (response. getEntity ()));
} Catch (ClientProtocolException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
    }
}

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.