HttpClient get and POST request processing

Source: Internet
Author: User

package cn.test1;import java.io.bufferedreader;import java.io.ioexception;import  java.io.inputstream;import java.io.inputstreamreader;import java.util.arraylist;import  java.util.list;import org.apache.http.httpentity;import org.apache.http.namevaluepair;import  org.apache.http.client.clientprotocolexception;import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.closeablehttpresponse;import  org.apache.http.client.methods.httpget;import org.apache.http.client.methods.httppost;import  Org.apache.http.impl.client.closeablehttpclient;import org.apache.http.impl.client.httpclients;import  org.apache.http.message.basicnamevaluepair;/** * 1. Creating an HttpClient object  * 2. Create an instance of the request method, and specify the request URL, create a HttpGet object if you need to send a GET request, or create a HttpPost object  * 3 if you need to send a POST request. If you need to send request parameters, You can call the HttpGet or HttpPost common SetParams (Httpparams params) method to add request parameters; *    for HttpPost objects andYou can also call the Setentity (httpentity entity) method to set the request parameters.  * 4. Call the Execute (httpurirequest request) of the HttpClient object to send the request, which returns a HttpResponse.  * 5. Call HttpResponse's Getallheaders (), Getheaders (String name), and so on to get the server's response header; *     Calls HttpResponse's GetEntity () method to get the Httpentity () object, which wraps the server's response content. The  *    program can get the response content of the server through this object.  * 6. Release the connection, you must release the connection, regardless of whether the method succeeds or not:  * */public class test1 {public static  void main (String[] args)  {get ();p OST ();} Private static void post ()  {//1. Creating HttpClient Objects Closeablehttpclient httpclient =  httpclients.createdefault (); try {//create HttpGet object//This path is wrong oh, the blind writing httppost httppost = new  httppost ("http://www.baidu.com/login.action");//create parameter queue list<namevaluepair> params =  New arraylist<namevaluepair> ();p Arams.add (New basicnamevaluepair ("username",  "admin")); Params.add (New basicnamevaluepair ("Password ", " 123456 ")); Urlencodedformentity formentity;formentity = new urlencodedformentity (params, "utf-8");// Sets the parameter to the HttpPost object Httppost.setentity (formentity); SYSTEM.OUT.PRINTLN ("Execute Request Path:" +httppost.geturi ());//execute GET request closeablehttpresponse response =  Httpclient.execute (HttpPost); SYSTEM.OUT.PRINTLN ("Response turntable" +response.getstatusline ());//Get response entity httpentity entity =  Response.getentity (); if (entity != null) {inputstream is = entity.getcontent (); String str = convertstreamtostring (IS); SYSTEM.OUT.PRINTLN ("Response content is:" +str); Httppost.abort ();}}  catch  (clientprotocolexception e)  {e.printstacktrace ();}  catch  (ioexception e)  {e.printstacktrace ();}} Private static void get ()  {//1. Creating HttpClient Objects Closeablehttpclient httpclient =  httpclients.createdefault (); try {//Create HttpGet object Httpget httpget = new httpget (" http://www.baidu.com/"); SYSTEM.OUT.PRINTLN ("Execute Request Path:" +httpget.geturi ());//execute GET request closeablehttpresponse response =  Httpclient.execute (HttpGet); SYSTEM.OUT.PRINTLN ("Response turntable" +response.getstatusline ());//Get response entity httpentity entity =  Response.getentity (); if (entity != null) {inputstream is = entity.getcontent (); String str = convertstreamtostring (IS); SYSTEM.OUT.PRINTLN ("Response content is:" +str); Httpget.abort ();}}  catch  (clientprotocolexception e)  {e.printstacktrace ();}  catch  (ioexception e)  {e.printstacktrace ();}} Private static string convertstreamtostring (Inputstream is)  {BufferedReader  Reader = new bufferedreader (New inputstreamreader (IS)); Stringbuilder sb = new stringbuilder (); String line = null;try {while ((Line = reader.readline ())  != null) { Sb.append (line+ "\ r \ n");}}  catch  (ioexception e)  {e.prinTstacktrace ();} Finally{try {is.close ();}  catch  (ioexception e)  {e.printstacktrace ();}} Return sb.tostring ();}}


This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1876229

HttpClient get and POST request processing

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.