Send an HTTP request to the server without waiting for a response

I need to send an HTTP POST request to the server, but it should not wait for a response. What method should I use for this?

I used

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(); 

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

If I use the request2.GetRespnse () command, I get a "Connection unexpectedly closed" error message

Any help would be appreciated. thanks

+4
source share
6 answers

If you are using HTTP, there should be a response.

However, this should not be a very large answer:

 HTTP/1.1 200 OK Date: insert date here Content-Length: 0 \r\n 
+6
source

refer to this .

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

+2
source

HTTP requires a response, as mentioned by Mike Caron. But as a quick (dirty) fix, you can catch the "Connnection closed unexpectedly" message and continue.

0
source

If your server is OK with this, you can always use a RAW socket to send a request and close it.

0
source

If you do not want to wait for a response, you can send data to another stream or just use WebClient.UploadStringAsync , but note that the response is always executed after the request. Using a different thread for the request ignores the response processing.

0
source

Take a look at this, this may help.

 public static void SetRequest(string mXml) { HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.CreateHttp("http://dork.com/service"); webRequest.Method = "POST"; webRequest.Headers["SOURCE"] = "WinApp"; // Decide your encoding here //webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentType = "text/xml; charset=utf-8"; // You should setContentLength byte[] content = System.Text.Encoding.UTF8.GetBytes(mXml); webRequest.ContentLength = content.Length; var reqStream = await webRequest.GetRequestStreamAsync(); reqStream.Write(content, 0, content.Length); var res = await httpRequest(webRequest); 

}

0
source

All Articles