f59134eeba5b43fdeb3262145b42de2e73e1b919
[jpim.git] / src / de / j32 / httplib / HttpPOSTRequest.java
1 package de.j32.httplib;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.OutputStreamWriter;
6 import java.io.Writer;
7
8 public class HttpPOSTRequest 
9         extends HttpRequest
10 {
11         boolean multipart_ = false;
12         static final String separator_ = 
13                 "----MultiPartFormData--MultiPartFormData--MultiPartFormData----";
14         
15         public HttpPOSTRequest(String url)
16         {
17                 super(url, "POST");
18                 setContentType("application/x-www-form-urlencoded; charset=utf-8");
19         }
20         
21         public HttpPOSTRequest setMultipart(boolean flag)
22         {
23                 multipart_ = flag;
24                 if (multipart_) {
25                         setContentType("multipart/form-data; boundary=" + separator_);
26                         try {
27                                 OutputStream body = body();
28                                 body.write("--".getBytes());
29                                 body.write(separator_.getBytes());
30                                 body.write("\r\n".getBytes());
31                         }
32                         catch (IOException e) {
33                                 throw new AssertionError("ByteArrayOutputStream throwing IOExcpetion");                 
34                         }
35                 }
36                 return this;
37         }
38         
39         @Override
40         public HttpRequest addParameter(String name, byte[] value, String encoding)
41         {
42                 try {
43                         if (multipart_) {
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());
53                                 body.write(value);
54                                 body.write("\r\n--".getBytes());
55                                 body.write(separator_.getBytes());
56                                 body.write("\r\n".getBytes());
57                         }
58                         else {
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);
62                                 writer.flush();
63                         }
64                 }
65                 catch (IOException e) {
66                         throw new AssertionError("ByteArrayOutputStream throwing IOExcpetion");                 
67                 }
68                 return this;
69         }
70 }