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.

1
  • Reading apache source may help you on how to get it done in vanilla java. Commented Jun 11, 2014 at 20:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
You should check this answer out 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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.