I've been trying for few hours now to send an HTTP POST using System.Net.HttpWebRequest but alas I fail.
From going through the source code, it appears that HttpWebRequest actually does not support sending body. Is it even possible?
Here is my code:
var webRequest = (HttpWebRequest) HttpWebRequest.Create(uri); webRequest.Method = "POST"; webRequest.ContentType = "text/html"; webRequest.ContentLength = 6; var requestStream = webRequest.GetRequestStream(); var length = requestStream.Length; var outputBuffer = Encoding.UTF8.GetBytes("ABCDEF"); requestStream.Write(outputBuffer, 0, outputBuffer.Length); requestStream.Close(); var webResponse = webRequest.GetResponse();
What I see is that a request being sent when I execute webRequest.GetRequestStream(), so by the time I get back a request stream - server has already sent a response (I have confirmed that with WireShark running on the server). And when I come to GetResponse - the response stream is empty, but that maybe a separate issue.
I can successfully do POST request using sockets, but I'd rather use HttpWebRequest.