HttpClient use Multipart/form-data type to upload files and forms __http

Source: Internet
Author: User


Finally found a ready to use


public static void Post () throws Clientprotocolexception, IOException {
Proxy p=new proxy ();
P.sethost ("192.168.1.153");
P.setport ("8888");

HttpClient httpclient = new Defaulthttpclient ();

Httphost proxy = new Httphost (P.gethost (), Integer.valueof (P.getport ()), "http");
Httpclient.getparams (). Setparameter (Connrouteparams.default_proxy, PROXY);
Request Path
HttpPost post = new HttpPost ("xxxxx");
Add Header header Information
Post.setheader ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1;) ";
Note Here must not add the Content-type:multipart/form-data property because there is a boundary parameter property is not controllable. This value is generated by the browser. If the force indicates and May
Causes the boundary value inconsistency to request the failure detail see http://blog.csdn.net/xiaojianpitt/article/details/6856536

Post.setheader ("Content-type", "Multipart/form-data");
Post.setheader ("Host", "* * *");
Post.setheader ("accept-encoding", "gzip");
Post.setheader ("CharSet", "Utf-8");
Filebody filebody = new Filebody (New File ("xxxx"), "Image/pjpeg", "utf-8");
multipartentity entity = new multipartentity ();
Add Message body Information
Entity.addpart ("xxxx", New stringbody ("xxx", Charset.forname ("Utf-8"));
Entity.addpart ("ImageFile", filebody);
Post.setentity (entity);
HttpResponse response = Httpclient.execute (POST);
if (Httpstatus.sc_ok==response.getstatusline (). Getstatuscode ()) {

Httpentity Entitys = response.getentity ();
if (entity!= null) {
System.out.println (entityutils.tostring (Entitys));
}
}
Httpclient.getconnectionmanager (). Shutdown ();
}

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.