B4J Question send data from b4j app to servlet

Roberto P.

Well-Known Member
Licensed User
Longtime User
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
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

Servlet trials

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

logs

Thanks in advance
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Erel,
thanks for your advice.
but it still doesn't work. Now I see that there are characters in the stream but I can't see them.

B4J:
aHttp.PostString("http://localhost:8080/hello/servlets/servlet/HelloWorld?username=roberto&password=mypassword", GetJSon)
aHttp.GetRequest.SetContentType("application/json" )

I also added the following piece of code in the servlet

servlet:
  String line = rd.readLine();
                 String message = new String();
              
                 while (line  != null) {
                     log("Leggo la linea del file.....");
                     rd.readLine();
                     message += line;
                 }
                
                 log("dati letti 2" + message);





do you have any idea?
thank
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
I've also tried using your TextReaderWrapper class, but throw an exception

B4J:
            TextReaderWrapper aReadeb4j = new TextReaderWrapper();
                aReadeb4j.Initialize(request.getInputStream());
                
                String aData = aReadeb4j.ReadAll();
                aReadeb4j.ReadLine();
                boolean aReady = aReadeb4j.Ready();


why?
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
SOLVED

B4J:
    aHttp.PostString("http://localhost:8080/Agon61/servlets/servlet/ServletSynchro?username=roberto&password=sitranica", GetJSon) '
    
    aHttp.GetRequest.SetContentEncoding("UTF-8")
    aHttp.GetRequest.SetContentType("application/text" )

servlet:
final StringBuilder builder = new StringBuilder();
                BufferedReader reader = request.getReader();
                
                 if (reader == null) {
                       log("Request body could not be read because it's empty.");
                    }
                    
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                    
                    log("Dati dal Client: " + builder.toString());


THANK EREL
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…