Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send http request to server without expecting a response

I have a need to send a POST http request to a server ,but it should not expect a response. What method should i use for it ?

I have been using

 WebRequest request2 = WebRequest.Create("http://local.ape-project.org:6969");
 request2.Method = "POST";
 String sendcmd = "[{\"cmd\":\"SEND\",\"chl\":3,\"params\":{\"msg\":\"Helloworld!\",\"pipe\":\"" + sub1 + "\"},\"sessid\":\"" + sub + "\"}]";
 byte[] byteArray2 = Encoding.UTF8.GetBytes(sendcmd);
 Stream dataStream2 = request2.GetRequestStream();
 dataStream2.Write(byteArray2, 0, byteArray2.Length);
 dataStream2.Close();
 WebResponse response2 = request2.GetResponse();

to send a request and get back a response. This works fine if the request will get a response back from the server. But, for my need, i just need to send a POST request. And there will be no response associated with the request i am sending. How do i do it ?

If i use the request2.GetRespnse() command , i get an error that "The connection was closed unexpectedly"

Any help will be appreciated. thanks

like image 648
CuriousCoder Avatar asked Jul 11 '11 03:07

CuriousCoder


3 Answers

If you're using the HTTP protocol, there has to be a response.

However, it doesn't need to be a very big response:

HTTP/1.1 200 OK
Date: insert date here
Content-Length: 0
\r\n
like image 164
Mike Caron Avatar answered Oct 19 '22 23:10

Mike Caron


refer to this answer.

What you are looking for, I think, is the Fire and Forget pattern.

like image 34
sTodorov Avatar answered Oct 19 '22 23:10

sTodorov


HTTP requires response as already mentioned by Mike Caron. But as a quick (dirty) fix you could catch the "connnection closed unexpectedly" error and continue.

like image 36
bala Avatar answered Oct 19 '22 23:10

bala