请求正文比客户端发送的短-HttpServer Java
我使用HttpServer创建Java应用程序,如下所示:请求正文比客户端发送的短-HttpServer Java,java,httpserver,Java,Httpserver,我使用HttpServer创建Java应用程序,如下所示: public class Application { public static void main(String args[]) { HttpServer httpPaymentServer; httpPaymentServer = HttpServer.create(new InetSocketAddress(Config.portPayment), 0); httpPaymentServer.create
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();
}
}
}
}
}
公共类应用程序
{
公共静态void main(字符串参数[])
{
HttpServer httpPaymentServer;
httpPaymentServer=HttpServer.create(新的InetSocketAddress(Config.portPayment),0);
httpPaymentServer.createContext(“/json”,新支付(“json”);
}
公共类支付实现HttpHandler
{
公共支付(字符串数据类型)
{
}
公共无效句柄(HttpExchange HttpExchange)引发IOException
{
字符串体=”;
if(httpExchange.getRequestMethod().equalsIgnoreCase(“POST”))
{
尝试
{
Headers requestHeaders=httpExchange.getRequestHeaders();
Set entries=requestHeaders.entrySet();
int contentLength=Integer.parseInt(requestHeaders.getFirst(“内容长度”));
InputStream InputStream=httpExchange.getRequestBody();
字节[]postData=新字节[contentLength];
int length=inputStream.read(postData,0,contentLength);
如果(长度<内容长度)
{
}
其他的
{
String fullBody=新字符串(postData);
Map query=Utility.splitQuery(全文);
body=query.getOrDefault(“数据”),toString();
}
}
捕获(例外e)
{
e、 printStackTrace();
}
}
}
}
}
在我的服务器(Centos 7)上,在第一次请求时,它没有问题。但在下一个请求中,并不是所有的请求主体都可以被读取。
但是在我的电脑(Windows10)上没有问题。
问题是什么 对于您的
InputStream
只调用read
一次-它可能不会返回所有数据。当时甚至可能没有收到该数据
相反,您应该在循环中调用
read
,直到获得所有字节(当您到达流的末尾read
返回-1)。或者使用此处建议的方法之一谢谢。这是我的工作
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();
}
}
}
公共无效句柄(HttpExchange HttpExchange)引发IOException
{
字符串体=”;
if(httpExchange.getRequestMethod().equalsIgnoreCase(“POST”))
{
尝试
{
Headers requestHeaders=httpExchange.getRequestHeaders();
Set entries=requestHeaders.entrySet();
int contentLength=Integer.parseInt(requestHeaders.getFirst(“内容长度”));
InputStream InputStream=httpExchange.getRequestBody();
int j;
字符串fullBody=“”;
对于(j=0;j
检查类实用程序中的splitQuery()
方法体。如果问题出在第二个请求上,可能是因为服务器在第一个请求上设置了cookie。检查可能影响splitQuery()的拆分逻辑的任何cookie数据