AppEngine:如何查看 POST 请求中的内容

发布于 2024-10-14 02:39:02 字数 401 浏览 3 评论 0原文

我向我的 AppEngine 服务器发送 POST 请求。 HttpServletRequest 告诉我:

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

我已经向世界发送了“HelloHelloHelloHelloHello”,根据内容长度,这是正确的。但是,我不知道如何恢复它。 你能解释一下吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-10-21 02:39:02

您应该给出参数名称和值,然后可以从 httpRequest 对象中提取参数。

request.getParameter("paramName");

更新

客户端

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();
}

另请参阅

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

秋意浓 2024-10-21 02:39:02

正如 Jigar 所说,您可以使用 request.getParameter() 。如果您确实提交表单或将参数指定为 URL 参数 (http://myhost/mypath?myparam=myvalue),则此方法有效。

如果您将数据作为 POST 正文发送,则应该从其正文中读取数据,即通过调用 request.getInputStream() 检索输入流,然后从此流中读取。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文