1

I've got a webserver setup ready to receive images and I'd like to have a client in Java send the image along with two POST arguments, upon searching the web I only found ways to do this with Apache's API but I'd prefer to do this in vanilla Java.

Any help will be appreciated.

Reddy360
  • 11
  • 1

1 Answers1

4

Something along the lines of...

String url = "https://asite.com";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

String urlParameters = "aparam=1&anotherparam=2";

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

You can add more headers, and add more to the output stream as required.

Matthew Trout
  • 709
  • 5
  • 20
  • I already know how to do that, sorry if I was a bit vague in me question but what I'm looking for is uploading the image, I know how to pass POST variables. – Reddy360 Jun 11 '14 at 20:48
  • You should check this answer out http://stackoverflow.com/a/11826317/2261980 It's quite lengthly but the real issue is that if you don't use an api, you're going to have to construct a lot of the wrappers yourself :) – Matthew Trout Jun 11 '14 at 20:58