I have a simple program either a server or client not sure which end it is. I open a port and accept messages on it. this works but it crashed last night when I revived a message via GET not POST. As you may have noticed I am very new to networking.
我有一个简单的程序服务器或客户端不知道它是哪一端。我打开一个端口并接受它上面的消息。这工作但昨晚我通过GET而不是POST恢复了一条消息时崩溃了。你可能已经注意到我对网络很新。
I am getting the transmission ok (the socket stuff) but i cant read the message.
我收到传输正常(套接字的东西)但我无法阅读该消息。
this is the code i currently use.
这是我目前使用的代码。
//read HTTp header until the message size comes in
for(int eight = 0; eight < 8; eight++)
{
message = in.readLine();
LOGGER.fatal(eight);
LOGGER.fatal(message);
if(message.contains("Content-Length"))
{
try {
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
sizeInt = Integer.parseInt(matcher.group());
}
}
catch (Exception e)
{
LOGGER.fatal("size not found on header line", e);
//System.exit(-1);
}
}
}
LOGGER.fatal(sizeInt);
LOGGER.fatal("---------------------------");
char[] buffer =new char[sizeInt];
//skip blank line between header and message
message = in.readLine();
//read message
int fullRead = 0;
int thisRead = 0;
do
{
thisRead = in.read(buffer, fullRead, sizeInt - fullRead);
LOGGER.info(fullRead + " of " + sizeInt + " bytes of message read");
fullRead += thisRead;
}while(fullRead != sizeInt);
The problem iv'e got is that the GET method does not seen to have the size of the message in bytes and without that I don't know how the read the message without it hanging.
我得到的问题是,GET方法没有看到以字节为单位的消息大小,没有它我不知道如何在没有它的情况下读取消息。
I'm using java.
我正在使用java。
Can anyone suggest how I can edit this code to understand a GET message.
任何人都可以建议我如何编辑此代码以理解GET消息。
3 个解决方案
#1
1
A GET request doesn't have any content - there's nothing to read. Any data has to be in the headers / URL.
GET请求没有任何内容 - 没有什么可读的。任何数据都必须位于标题/ URL中。
#2
1
It looks like you want to implement an HTTP server (it's not a client, because you want to read HTTP request, not make them). If you're "very new to networking", I wouldn't recommend it. Anyway, if you really want to build your own HTTP server there are examples on the Web, for instance this one. Another approach, if you need an HTTP server in your application is to use com.sun.net.httpserver.HttpServer
(the link include a minimal example).
看起来你想要实现一个HTTP服务器(它不是一个客户端,因为你想要读取HTTP请求,而不是它们)。如果你是“非常新的网络”,我不会推荐它。无论如何,如果你真的想要构建自己的HTTP服务器,那么Web上有一些例子,例如这个例子。另一种方法是,如果您的应用程序中需要HTTP服务器,则使用com.sun.net.httpserver.HttpServer(该链接包含一个最小的示例)。
#3
0
HTTP GET does not have any content, and therefor does not have a content-length. Also, a HTTP POST does not necessarily need to specify Content-Length in HTTP/1.1: HTTP Message Length
HTTP GET没有任何内容,因此没有内容长度。此外,HTTP POST不一定需要在HTTP / 1.1中指定Content-Length:HTTP消息长度
#1
1
A GET request doesn't have any content - there's nothing to read. Any data has to be in the headers / URL.
GET请求没有任何内容 - 没有什么可读的。任何数据都必须位于标题/ URL中。
#2
1
It looks like you want to implement an HTTP server (it's not a client, because you want to read HTTP request, not make them). If you're "very new to networking", I wouldn't recommend it. Anyway, if you really want to build your own HTTP server there are examples on the Web, for instance this one. Another approach, if you need an HTTP server in your application is to use com.sun.net.httpserver.HttpServer
(the link include a minimal example).
看起来你想要实现一个HTTP服务器(它不是一个客户端,因为你想要读取HTTP请求,而不是它们)。如果你是“非常新的网络”,我不会推荐它。无论如何,如果你真的想要构建自己的HTTP服务器,那么Web上有一些例子,例如这个例子。另一种方法是,如果您的应用程序中需要HTTP服务器,则使用com.sun.net.httpserver.HttpServer(该链接包含一个最小的示例)。
#3
0
HTTP GET does not have any content, and therefor does not have a content-length. Also, a HTTP POST does not necessarily need to specify Content-Length in HTTP/1.1: HTTP Message Length
HTTP GET没有任何内容,因此没有内容长度。此外,HTTP POST不一定需要在HTTP / 1.1中指定Content-Length:HTTP消息长度