httpclient 4.3 post form with parameters, submit file/binary data

Source: Internet
Author: User
Tags http post throw exception

HttpClient HttpClient = Httpclients.createdefault (); HttpPost HttpPost = new HttpPost (URL); Multipartentitybuilder Mentitybuilder = Multipartentitybuilder.create ();//byte[] Postbodymentitybuilder.addbinarybody (Postname, postbody);//Submit file//file files = new file ("test");// Mentitybuilder.addbinarybody ("name", file), Mentitybuilder.addtextbody ("name", "Value"), Httppost.setentity ( Mentitybuilder.build ()); HttpResponse responce = Httpclient.execute (HttpPost);

Not written as interface: can be written directly together

Httpentity reqentity = Multipartentitybuilder.create (). SetMode (httpmultipartmode.browser_compatible). AddPart (" Multipartfile ", bin). Addpart (" userid ", UserID). Setcharset (Charsetutils.get (" UTF-8 ")). Build ();

Without parameters: The specified entity can be defined directly

File File = new file ("Somefile.txt"); Fileentity reqentity = new Fileentity (file, Contenttype.create ("Text/plain", "UTF-8")); byte[] B; bytearrayentity entity = new Bytearrayentity (b);

Here are the interfaces I define myself:

/** * Http request:post * * @param URL * @param postbody (Byte) * @param postname * @param params * @param heads * @param TimeOut (Millisecond) * @return String of request result */public static string postfile (string URL, byte[] postbody, Strin  G Postname, Map params,map heads, Integer timeOut) throws Httperrorexception {String restr = ""; try {HttpClient HttpClient = Httpclients.createdefault (); HttpPost HttpPost = new HttpPost (URL); Multipartentitybuilder Mentitybuilder = Multipartentitybuilder.create (); Mentitybuilder.addbinarybody (PostName, Postbody); if (params! = null) {//Text paramsfor (Entry e:params.entryset ()) {Mentitybuilder.addtextbody (E.getkey (), e.g Etvalue ());}} Httppost.setentity (Mentitybuilder.build ()); if (heads! = null) {//General requirements プロパティを set しますfor (Entry e:heads.entryset ()) { Httppost.addheader (E.getkey (), E.getvalue ());}} Set Timeoutrequestconfig Requestconfig = Requestconfig.custom (). Setconnectionrequesttimeout (TimeOut). Setconnecttimeout (timeout). SetSocketTimeout (timeout).Build (); Httppost.setconfig (requestconfig);//Get Responcehttpresponse responce = Httpclient.execute (HttpPost);//Get  httpstatus codeint Resstatu = Responce.getstatusline (). Getstatuscode (); if (Resstatu = = HttpStatus.SC_OK) {//Get result datahttpentity entity = responce.getentity (); restr = entityutils.tostring (entity);} else {log.error (url + ": Resstatu is" + Resstatu); throw new Httperrorexception (URL, ' Resstatu is ' + Resstatu);}} catch (Connectionpooltimeoutexception e) {log.error ("http Post throw connectionpooltimeoutexception", e); throw new Httperrorexception (URL, "throw timeout");} catch (Connecttimeoutexception e) {log.error ("http Post throw connecttimeoutexception", e); throw new Httperrorexception (URL, "throw timeout");} catch (Sockettimeoutexception e) {log.error ("http Post throw sockettimeoutexception", e); throw new Httperrorexception ( URL, "throw timeout");} catch (Httperrorexception e) {throw e;} catch (Exception e) {log.error ("http Post throw Exception", e), throw new Httperrorexception (URL, "throw Exception");} return restr;}  

HttpClient 4.3 post form with parameters, submit file/binary data

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.