Java HttpClient POST request

Source: Internet
Author: User

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&LT     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

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.