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