Java 在 NanoHTTPD 中检索 HTTP 正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22349772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve HTTP Body in NanoHTTPD
提问by miho
How can I retrieve the HTTP POSTrequest body when implementing NanoHTTPDs servemethod?
POST实现NanoHTTPD的serve方法时如何检索 HTTP请求正文?
I've tried to use the getInputStream()method of IHTTPSessionalready, but I always get an SocketTimeoutExceptionwhen using it inside of the servemethod.
我已经尝试使用 的getInputStream()方法IHTTPSession,但是SocketTimeoutException在serve方法内部使用它时我总是得到一个。
采纳答案by bob esponja
In the servemethod you first have to call session.parseBody(files), where filesis a Map<String, String>, and then session.getQueryParameterString()will return the POSTrequest's body.
在该serve方法中,您首先必须调用session.parseBody(files),其中files是 a Map<String, String>,然后session.getQueryParameterString()将返回POST请求的正文。
I found an examplein 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.
}
回答by Rin malavi
On a IHTTPSessioninstance you can call the .parseBody(Map<String, String>)method which will then fill the map you provided with some values.
在IHTTPSession实例上,您可以调用该.parseBody(Map<String, String>)方法,然后该方法将用一些值填充您提供的地图。
Afterwards your map may contain a value under the key postBody.
之后,您的地图可能会在 key 下包含一个值postBody。
final HashMap<String, String> map = new HashMap<String, String>();
session.parseBody(map);
final String json = map.get("postData");
This value will then hold your posts body.
该值将保存您的帖子正文。
回答by khaintt
I think session.getQueryParameterString();not work in this case.
我认为session.getQueryParameterString();在这种情况下不起作用。
If you using POST, PUT, you should want to try this code:
如果您使用POST, PUT,您应该尝试使用以下代码:
Integer contentLength = Integer.parseInt(session.getHeaders().get("content-length"));
byte[] buffer = new byte[contentLength];
session.getInputStream().read(buffer, 0, contentLength);
Log.d("RequestBody: " + new String(buffer));
In fact, I tried IOUtils.toString(inputstream, encoding)but it cause Timeout exception!
事实上,我尝试过,IOUtils.toString(inputstream, encoding)但它导致Timeout exception!

