A simple httpclient POST request instance
Package Com.httpclientget;import Java.awt.list;import Java.util.arraylist;import org.apache.http.client.HttpClient ; Import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.httpclients;import Org.apache.http.message.basicnamevaluepair;import Org.apache.http.util.entityutils;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;public class Testpost {public static void main (string[] args) {Creathttpurl Creaturl = new C Reathttpurl (); String urlstr = "Https://openapi.youdao.com/api"; String paramstr = Creaturl.createparam ("Good", "en", "zh-chs"); System.out.println (PARAMSTR); new Post (). Start ();}} Class Post extends Thread{httpclient client = Httpclients.createdefault (); @Override public void Run () {HttpPost post = new HttpPost ("Https://openapi.youdao.com/api"); try {//post The parameter to be submitted arraylist<namevaluepair> params = new ARRAYLIST< Namevaluepair> (); Params.add (New Basicnamevaluepair ("Q", "good")); Params.add (New Basicnamevaluepair ("Salt", "1496238482428")); Params.add (New Basicnamevaluepair ("sign", "02e15cdaf871b698fe04ee32fd2cf155")); Params.add (New Basicnamevaluepair ("from", "en")); Params.add (New Basicnamevaluepair ("AppKey", "7743eee7f7e11d75")); Params.add (New Basicnamevaluepair ("to", "Zh-chs")); Set the commit encoding post.setentity (new urlencodedformentity (params, "UTF-8")); Create response receive return value HttpResponse response = Client.execute (POST); httpentity entity = response.getentity (); String result = entityutils.tostring (entity, "UTF-8"); System.out.print (result);} catch (Exception e) {e.printstacktrace ();} }}
Java HttpClient POST request