1 package de.j32.httplib;
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.OutputStreamWriter;
8 public class HttpPOSTRequest
11 boolean multipart_ = false;
12 static final String separator_ =
13 "----MultiPartFormData--MultiPartFormData--MultiPartFormData----";
15 public HttpPOSTRequest(String url)
18 setContentType("application/x-www-form-urlencoded; charset=utf-8");
21 public HttpPOSTRequest setMultipart(boolean flag)
25 setContentType("multipart/form-data; boundary=" + separator_);
27 OutputStream body = body();
28 body.write("--".getBytes());
29 body.write(separator_.getBytes());
30 body.write("\r\n".getBytes());
32 catch (IOException e) {
33 throw new AssertionError("ByteArrayOutputStream throwing IOExcpetion");
40 public HttpRequest addParameter(String name, byte[] value, String encoding)
44 OutputStream body = body();
45 body.write("Content-Disposition: form-data; name=\"".getBytes());
46 body.write(name.getBytes());
47 body.write("\"\r\n".getBytes());
48 body.write("Content-Type: text/plain; charset=".getBytes());
49 body.write(encoding.getBytes());
50 body.write("\r\n".getBytes());
51 body.write(("Content-Length: " + value.length).getBytes());
52 body.write("\r\n\r\n".getBytes());
54 body.write("\r\n--".getBytes());
55 body.write(separator_.getBytes());
56 body.write("\r\n".getBytes());
59 // Encoding not really relevant here since url-encoding is plain ASCII
60 Writer writer = new OutputStreamWriter(body(),"ascii");
61 appendParameter(writer, body().size() == 0, name, value);
65 catch (IOException e) {
66 throw new AssertionError("ByteArrayOutputStream throwing IOExcpetion");