我怎样才能检索HTTP POST执行时要求身体NanoHTTPD小号serve的方法?
我已经尝试使用已经的getInputStream()方法IHTTPSession,但我总是得到一个SocketTimeoutException在方法中使用它serve.
bob*_*nja 25
在serve您首先必须调用的方法中session.parseBody(files),where files是a Map<String, String>,然后session.getQueryParameterString()将返回POST请求的正文.
我在源代码中找到了一个例子.这是相关代码:
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.
}
Run Code Online (Sandbox Code Playgroud)
Rin*_*avi 19
在一个IHTTPSession实例上,您可以调用该.parseBody(Map<String, String>)方法,然后使用某些值填充您提供的地图.
之后,您的地图可能包含密钥下的值postBody.
final HashMap<String, String> map = new HashMap<String, String>();
session.parseBody(map);
final String json = map.get("postData");
Run Code Online (Sandbox Code Playgroud)
然后,此值将保留您的帖子正文.
我认为session.getQueryParameterString();在这种情况下不起作用.
如果你使用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));
Run Code Online (Sandbox Code Playgroud)
事实上,我试过IOUtils.toString(inputstream, encoding)但它导致了Timeout exception!
| 归档时间: |
|
| 查看次数: |
14819 次 |
| 最近记录: |