首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >请求正文比客户端发送的短- HttpServer Java

请求正文比客户端发送的短- HttpServer Java
EN

Stack Overflow用户
提问于 2018-09-16 22:31:28
回答 2查看 303关注 0票数 2

我使用HttpServer创建Java Application,如下所示:

代码语言:javascript
复制
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(); 
            }    
        }
    }
}
}

在我的服务器(Centos 7)上,对于第一个请求,这是没有问题的。但是在下一个请求中,并不是所有的请求正文都可以读取。但在我的电脑(Windows10)上没有问题。问题出在哪里?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-16 23:57:05

对于您的InputStream,只需调用read一次-它可能不会返回所有数据。该数据甚至可能在那时没有被接收到。

相反,您应该在循环中调用read,直到获得所有字节(当您到达流的末尾时,read返回-1)。或者使用这里建议的方法之一How to read / convert an InputStream into a String in Java?

票数 1
EN

Stack Overflow用户

发布于 2018-09-17 01:43:30

谢谢。这对我来说很有效

代码语言:javascript
复制
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(); 
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52355116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档