On httpclient upload with parameters "commons-httpclient and Apache httpclient difference"

Source: Internet
Author: User

Need to do a httpclient upload, and then snapped online for information

1. First used in the system before the Commons-httpclient upload, find the information after a meal change, and then test

Postmethod filepost = new Postmethod (URL); Filepost.setparameter ("System", "Vinuxpost"); try {part part[] = Uploadrequesthelper.getpart (request); Filepost.setrequestentity (New Multipartrequestentity (part, Filepost.getparams ())); HttpClient client = new HttpClient (); Client.gethttpconnectionmanager (). Getparams (). Setconnectiontimeout (n); int Status = Client.executemethod (FilePost); SYSTEM.OUT.PRINTLN (status);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}

One of the main code, upload the file way with a byte, and then take the required parameters, and then encountered a strange problem, that is, the parameter is not over, and then removed the upload of the file part, the parameters came over, it is strange, with an afternoon, the Internet to find information or no solution, there is no solution!!! I hope someone can tell me, hahaha, maybe it is the jar package version of the problem, I see a lot of information on the Internet can be with parameters to upload files.

I hope the great God can help me solve the above problems.

2. Then Apache httpclient upload, on the code

Closeablehttpclient httpClient = Httpclients.createdefault (); HttpPost HttpPost = new HttpPost (URL); list<multipartfile> listfile = uploadrequesthelper.getlistmultipartfile (request);//Create the form field content text to be processed stringbody system = new Stringbody ("Vinuxpost"); Multipartentity reqentity = new multipartentity (); for (Multipartfile file:listfile) {bytearraybody bab = new ByteArrayBo Dy (File.getbytes (), File.getname ()) Reqentity.addpart (File.getname (), Bab);} Reqentity.addpart ("System", System); httppost.setentity (reqentity); Closeablehttpresponse response = Httpclient.execute (HttpPost); System.out.println ("Status:" + Response.getstatusline ());

No accident, success, you can continue the following work, hahaha

Summary of the differences between Commons-httpclient and Apache HttpClient:

First of all, commons-httpclient seems to be rarely updated, and the document is not very sufficient, online examples of many are repeated, Apache HttpClient has been updated, haha, more information!!!

Stick Apache httpclient maven Hey, here it is.

    <Dependency>        <groupId>Org.apache.httpcomponents</groupId>        <Artifactid>HttpClient</Artifactid>        <version>4.3.3</version>    </Dependency>        <Dependency>        <groupId>Org.apache.httpcomponents</groupId>        <Artifactid>Httpmime</Artifactid>        <version>4.2.1</version>    </Dependency>

Need Httpmime, don't forget.

On httpclient upload with parameters "commons-httpclient and Apache httpclient differences"

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.