1

I send a POST request to my AppEngine server. The HttpServletRequest says me :

POST /connexionDeconnexion HTTP/1.1
User-Agent: Java/1.6.0_20
Host: localhost:8888
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 25

I have sent the world "HelloHelloHelloHelloHello", which is correct according to the Content-Length. However, I don't know how to recover it. Can you explain me ?

g123k
  • 3,476
  • 6
  • 38
  • 44

2 Answers2

5

As Jigar said you can use request.getParameter(). This works if you really submit the form or specify parameter as a URL argument (http://myhost/mypath?myparam=myvalue).

If you send your data as a POST body you should read it from its body, i.e. retrieve input stream by calling request.getInputStream() and then read from this stream.

AlexR
  • 109,181
  • 14
  • 116
  • 194
  • `request.getParameter()` will also work if you post the way I have suggested. – jmj Jan 24 '11 at 10:04
  • And if I would like to send a Picture and a String, what do I need to do ? One Object per parameter ? Two requests ? – g123k Jan 24 '11 at 15:12
1

You shoudl give param name and value and then you can extract param from httpRequest object.

request.getParameter("paramName");

Update

client side

String param = "value";
File textFile = new File("/path/to/file.txt");
File binaryFile = new File("/path/to/file.bin");
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.

URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
PrintWriter writer = null;
try {
    OutputStream output = connection.getOutputStream();
    writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important!

    // Send normal param.
    writer.println("--" + boundary);
    writer.println("Content-Disposition: form-data; name=\"param\"");
    writer.println("Content-Type: text/plain; charset=" + charset);
    writer.println();
    writer.println(param);

    // Send text file.
    writer.println("--" + boundary);
    writer.println("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"");
    writer.println("Content-Type: text/plain; charset=" + charset);
    writer.println();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(textFile), charset));
        for (String line; (line = reader.readLine()) != null;) {
            writer.println(line);
        }
    } finally {
        if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
    }

    // Send binary file.
    writer.println("--" + boundary);
    writer.println("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"");
    writer.println("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName());
    writer.println("Content-Transfer-Encoding: binary");
    writer.println();
    InputStream input = null;
    try {
        input = new FileInputStream(binaryFile);
        byte[] buffer = new byte[1024];
        for (int length = 0; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
        output.flush(); // Important! Output cannot be closed. Close of writer will close output as well.
    } finally {
        if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
    }
    writer.println(); // Important! Indicates end of binary boundary.

    // End of multipart/form-data.
    writer.println("--" + boundary + "--");
} finally {
    if (writer != null) writer.close();
}

Also See

Community
  • 1
  • 1
jmj
  • 225,392
  • 41
  • 383
  • 426
  • Thanks for your answer. If I have sent this String : writer = new OutputStreamWriter(conn.getOutputStream()); writer.write("HelloHelloHelloHelloHello"); What is the parameter ? When using the debug mode, the _parameters field is null. Is-it normal ? – g123k Jan 24 '11 at 09:44
  • Thanks for your answer. If I would like to send a picture (a car) and a String ("Chevrolet"), do I need to send 2 requests with 2 different parameters ? I know that my question is stupid but the document about appengine is very small – g123k Jan 24 '11 at 15:09
  • A last question :) Your link talks about a getParts method. However this function is not implemented with AppEngine. How can I know, what is the normal param, the text file... ? Thanks again :) – g123k Jan 24 '11 at 16:26
  • I would like to know, you are confused on client part or server part ? – jmj Jan 24 '11 at 16:57
  • The server part. If I use your client code, how to get data on the server part ? I mean, how to differentiate the text file from the binary file (for example) ? – g123k Jan 24 '11 at 17:44
  • lets make it easy : check [this](http://www.jguru.com/faq/view.jsp?EID=62798) simple solution comprises of both client and server – jmj Jan 24 '11 at 17:48