在NanoHTTPD中检索HTTP正文 [英] Retrieve HTTP Body in NanoHTTPD

查看:386
本文介绍了在NanoHTTPD中检索HTTP正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在实施 NanoHTTPD <$ c时检索HTTP POST 请求正文$ c>发送方法?

How can I retrieve the HTTP POST request body when implementing NanoHTTPDs serve method?

我试图使用 getInputStream() IHTTPSession 的方法已经存在,但在中使用它时总是得到 SocketTimeoutException 发送方法。

I've tried to use the getInputStream() method of IHTTPSession already, but I always get an SocketTimeoutException when using it inside of the serve method.

推荐答案

服务中首先必须调用 session.parseBody(files)的方法,其中 files 映射< String,String> ,然后 session.getQueryParameterString()将返回 POST 请求的正文。

In the serve method you first have to call session.parseBody(files), where files is a Map<String, String>, and then session.getQueryParameterString() will return the POST request's body.

我发现源代码中的示例。以下是相关代码:

I found an example in the source code. Here is the relevant code:

public Response serve(IHTTPSession session) {
    Map<String, String> files = new HashMap<String, String>();
    Method method = session.getMethod();
    if (Method.PUT.equals(method) || Method.POST.equals(method)) {
        try {
            session.parseBody(files);
        } catch (IOException ioe) {
            return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
        } catch (ResponseException re) {
            return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
        }
    }
    // get the POST body
    String postBody = session.getQueryParameterString();
    // or you can access the POST request's parameters
    String postParameter = session.getParms().get("parameter");

    return new Response(postBody); // Or postParameter.
}

这篇关于在NanoHTTPD中检索HTTP正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆