将http请求发送到服务器而不期待响应

我需要向服务器发送POST http请求,但它不应该期待响应。 我应该用什么方法呢?

我一直在用

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

发送请求并获得回复。 如果请求将从服务器返回响应,这可以正常工作。 但是,根据我的需要,我只需要发送一个POST请求。 并且没有与我发送的请求相关的响应。 我该怎么做 ?

如果我使用request2.GetRespnse()命令,我收到“连接意外关闭”的错误

任何帮助将不胜感激。 谢谢

如果您正在使用HTTP协议,则必须有响应。

但是,它不需要是一个非常大的响应:

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

参考这个答案。

我认为,你正在寻找的是火和忘记模式。

HTTP需要响应,如Mike Caron已经提到的那样。 但作为一个快速(脏)修复,你可以抓住“连接意外关闭”错误并继续。

如果你的服务器没问题,你总是可以使用RAW套接字发送请求然后关闭它。

如果您不想等待响应,可以在另一个线程中发送数据或简单地使用WebClient.UploadStringAsync ,但请注意,响应始终在请求后发生。 使用另一个请求线程可以忽略响应处理。

看看这可能会有所帮助。

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

}