How to send HTTP request response | Telit Cinterion IoT Developer Community
September 24, 2018 - 2:03pm, 2455 views
Hello all.
I have an application which is listening for http requests in a specified socket.
Everything works fine (InputStream open, receives the data, and i can do the logical business that I'm supposed to with the information that I got on the request) but i need to build a response to the request (the request returns that it doesn't know the request response) because it keeps hanging despite of the fact that it has been delivered.
Could you please provide an example for this scenario?
Thank you in advance
Can someone help me providing an example of a webserver which listens to http requests and provides a response to the said requests?
Thank you.
Hello,
This forum is about cellular M2M modules not about webservers. If your question is not related to any wireless 2G/3G/4G module you'll probably not find anyody able to help you.
Regards,
Bartłomiej
This is related with the BGS5T.
The webserver is running on the modem... Thought that would be an obvious information. My bad.
OK, so please provide some more details about the problem. Is the server in Java or AT commands? The server socket in Java or listenr in case of AT commands is just a bare socket. So you must have your own http implementation. Please provide some log maybe or other example to show what exactly the problem is. You create some response on the module and try to send it and the application hangs?
Hello again,
I'm using the server in Java. Here's what I'm doing:
//Opening the socket
server = (ServerSocketConnection) Connector
.open(...);
SocketConnection sc = (SocketConnection) server
.acceptAndOpen();
//Opening InputStream
is = sc.openInputStream();
//Opening Output Stream
os = sc.openOutputStream();
//If there's any data I read it
if ((is.available()>0))
{
readData=is.read(buffer);
for (int i=0;i<readData;i++)
String stream= stream + (char)buffer[i];
.... (The rest of logical operation with the http request stored on the String stream.)
//And once I confirm that the message in the http request was delivered at the device attached to the BGS5T, I try to make an HTTP response.
String httpStream ="<HTTP><HEAD></HEAD><BODY>"+ "RCV_OK" + "</BODY></HTTP>";
sendData(streamHTTP.getBytes(),streamHTTP.length());
private boolean sendData(byte buffer[], int length)
{
try
{
os.write(buffer, 0, length);
os.flush();
return true;
}
So, I'm currently experiencing two issues:
1. The client doesn't receive my response. Is this the correct way to do it? If not, how should I do it? Is there any example?
2. In the major part of clients, I'm being able to receive the whole http request (header + body )and store it in the stream string. Although, in a new client using that uses the .net http request library, the body and header are sent in two different packages. What should I do to get the body? I'm only being able to get the header.
An example for handling http requests (receive and respond) would be great.
Thank you
Hello,
Thank you for the details.
I can't see anything bad in your data sending method. It's correct to use flush() method, however you should be aware that according to API the flush method of OutputStream does nothing. And the response is not provided even if you close the stream and connection?
As your client is http client and it expects to connect to http server maybe the problem is that the reply is sent back to the client but it does not meet the http protocol requirements, does not fit the typical web server reply and that's why it is ignored by the client in some underlying layer before it can reach your code.
I think that if the body is sent with some delay after the header your code should wait for more data checking if it is available with is.available()>0 condition or in.read(buffer)) != -1 condition.
Currently we probably don't have any http implementation example. However you will find some demo projects that uses socket connections in the Knowledge Base.
Best regards,
Bartłomiej
Hello Bartlomiej.
That was it.
I needed to let my code wait a little more, and once I got the header, I had to send the 100-continue response.
Thank you.