I am writing a simple snippet which sends a simple post request.
Currently I am building the request like so:
// Construct data
String data = URLEncoder.encode("param1", "UTF-8") + "=" + URLEncoder.encode("val1", "UTF-8");
data += "&" + URLEncoder.encode("param2", "UTF-8") + "=" + URLEncoder.encode("val2", "UTF-8");
// Send data
URL url = new URL("http://server:8080/servlet/upload");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// do stuff with response....
This works, as of now. But I need to add a file upload as a multipart POST request.
How can I do this? I would like to avoid using HttpClient
from commons if possible.