0

So I've struggling with this one lately. There are multiple ways to make a HTTP POST request with Java, some of which demonstrate with HttpClient 3.x and the others with HttpClient 4.x

I'm assuming that the 4.x line is more advanced, but there are also lacking tutorials with this one. At least for me, I had hard time to find some beginner friendly guides.

I'd like to make a POST request with an encoded File object.

Some of the tutorials I've followed also mentioned NameValuePair, but for me this class is not available and I don't know how to obtain it. So if you answer, please mention how I can get hold of the imports as well.

Please demonstrate a minimal HTTP POST request with HttpClient 4.x

Jan
  • 13,738
  • 3
  • 30
  • 55
aborted
  • 4,481
  • 14
  • 69
  • 132
  • 1
    `NameValuePair` is in the same library as `HttpClient`, that is [the HTTP components httpclient](http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient). – Sotirios Delimanolis Jun 23 '14 at 15:21
  • Also, to send a POST with a file, you'll want to do a multipart request. You can do that with [`MultipartEntityBuilder`](http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html) which is part of [mime](http://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime). – Sotirios Delimanolis Jun 23 '14 at 15:24
  • Check this example which uses Apache's library http://evgenyg.wordpress.com/2010/05/01/uploading-files-multipart-post-apache/ – Nikhil Talreja Jun 23 '14 at 15:24
  • @SotiriosDelimanolis I can't seem to have that figured out. What's the actual import for this? – aborted Jun 23 '14 at 15:25
  • `org.apache.http.NameValuePair` and its basic sub type `org.apache.http.message.BasicNameValuePair`. Don't use this for file uploads. – Sotirios Delimanolis Jun 23 '14 at 15:26
  • @SotiriosDelimanolis Thanks. What should be used for file uploads instead? – aborted Jun 23 '14 at 15:30
  • 1
    See my second comment. – Sotirios Delimanolis Jun 23 '14 at 15:31
  • Take a look at this Post. It should explain things to you - http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java – Sir Geek Aug 15 '14 at 14:57

0 Answers0