3

I create Java Application using HttpServer as bellow:

public class Application 
{
public static void main(String args[])
{
    HttpServer httpPaymentServer;
    httpPaymentServer = HttpServer.create(new InetSocketAddress(Config.portPayment), 0);
    httpPaymentServer.createContext("/json", new Payment("json"));
}
public class Payment implements HttpHandler
{
    public Payment(String dataType)
    {
    }
    public void handle(HttpExchange httpExchange) throws IOException 
    { 
        String body = "";
        if(httpExchange.getRequestMethod().equalsIgnoreCase("POST")) 
        {
            try 
            {
                Headers requestHeaders = httpExchange.getRequestHeaders();
                Set<Map.Entry<String, List<String>>> entries = requestHeaders.entrySet();
                int contentLength = Integer.parseInt(requestHeaders.getFirst("Content-length"));
                InputStream inputStream = httpExchange.getRequestBody();
                byte[] postData = new byte[contentLength];
                int length = inputStream.read(postData, 0, contentLength);
                if(length < contentLength)
                {                   
                }
                else
                {
                    String fullBody = new String(postData);                 
                    Map<String, String> query = Utility.splitQuery(fullBody);
                    body = query.getOrDefault("data", "").toString();
                }
            } 
            catch (Exception e) 
            {
                e.printStackTrace(); 
            }    
        }
    }
}
}

On my server (Centos 7), on the first request, it is no problem. But on next request, not all of the request body can be read. But on my PC (Windows 10) no problem. What is the problem.

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80

2 Answers2

1

For your InputStream you call read only once - it may not return all the data. That data may even be not received at that time.

Instead you should call read in a loop until you get all the bytes (when you reach end of stream read returns -1). Or use one of the approaches suggested here How to read / convert an InputStream into a String in Java?

algrid
  • 5,600
  • 3
  • 34
  • 37
0

Thank you. This work for me

public void handle(HttpExchange httpExchange) throws IOException 
{
    String body = "";
    if(httpExchange.getRequestMethod().equalsIgnoreCase("POST")) 
    {
        try 
        {
            Headers requestHeaders = httpExchange.getRequestHeaders();
            Set<Map.Entry<String, List<String>>> entries = requestHeaders.entrySet();
            int contentLength = Integer.parseInt(requestHeaders.getFirst("Content-length"));
            InputStream inputStream = httpExchange.getRequestBody();             
            int j;
            String fullBody = "";
            for(j = 0; j < contentLength; j++)
            {
                byte b = (byte) httpExchange.getRequestBody().read();
                fullBody += String.format("%c", b);
            }
            Map<String, String> query = Utility.splitQuery(fullBody);
            body = query.getOrDefault("data", "").toString();
        } 
        catch (Exception e) 
        {
            e.printStackTrace(); 
        }
    }
}
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98