from an app created with B4J, I have to send a json text to a tomcat servlet, but the stream seems not to receive the data.
someone can tell me where I'm wrong.
Here is the code.
B4J
[CODE lang="b4x" title="B4J"]Dim aHttp As HttpJob
aHttp.Initialize("send_data2", Me)
aHttp.PostString("http://localhost:8080/hello/servlets/servlet/HelloWorld?username=roberto&password=mypassword", GetJSon)
Wait For (aHttp) JobDone(j As HttpJob)
If j.Success Then
LogError("chiamata avvenuta con successo x invio bytes")
LogError("ritorno server: ")
Log(j.GetString)
j.Release
Else
LogError("Chiamata servelet fallita SendObject")
End If[/CODE]
Servlet trials
[CODE lang="b4x" title="servlet"]
//---------------------------------------------------------------------------------------------------------------
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doPost(request, response);
Date date = new Date();
PrintWriter out = response.getWriter();
out.println("risposta del server con DoPost....del " + date.toString() + " " + date.getTime() );
log("risposta del server con DoPost....del " + date.toString() + " " + date.getTime() );
// this.ReadParameters(request);
String username = request.getParameter("username");
String password = request.getParameter("password");
out.println("username " + username + " " + "password = " + password);
log("username " + username + " " + "password = " + password);
int nRead2 = request.getContentLength();
log( " lenght content = " + nRead2);
log("type content = " + request.getContentType());
log("getContextPath " + request.getContextPath());
BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
ServletInputStream sis = request.getInputStream();
log("To string input stream: " + sis.toString());
int nAvailable = sis.available();
log("Number available: " + nAvailable);
int nRead = sis.read();
log("Numero di caratteri in canna: " + nRead);
byte[] bt = new byte[1024];
sis.read(bt);
log("Lunghezza read byte: " + bt.length);
char[] buffer = new char['?'];
StringBuilder sb = new StringBuilder(1024);
int count;
while ((count = rd.read(buffer, 0, buffer.length)) != -1)
{
// int count;
if (count < buffer.length) {
sb.append(new String(buffer, 0, count));
} else {
sb.append(buffer);
}
}
log("dati letti " + sb.toString());
}[/CODE]
logs
29-Dec-2019 15:03:24.803 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: risposta del server con DoPost....del Sun Dec 29 15:03:24 CET 2019 1577628204803
29-Dec-2019 15:03:24.803 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: username roberto password = mypassword
29-Dec-2019 15:03:24.804 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: lenght content = 62
29-Dec-2019 15:03:24.804 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: type content = application/x-www-form-urlencoded
29-Dec-2019 15:03:24.804 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: getContextPath /hello
29-Dec-2019 15:03:24.804 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: To string input stream org.apache.catalina.connector.CoyoteInputStream@3d57bc23
29-Dec-2019 15:03:24.804 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: Number available 0
29-Dec-2019 15:03:24.804 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: Numero di caratteri in canna -1
29-Dec-2019 15:03:24.804 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: Lunghezza read byte 1024
29-Dec-2019 15:03:24.804 INFORMAZIONI [http-nio-8080-exec-6] org.apache.catalina.core.ApplicationContext.log HelloWorld: dati letti
Thanks in advance