Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

farmr

Member Since 22 Apr 2015
Offline Last Active Apr 23 2015 05:37 PM
-----

Topics I've Started

Is it possible to send a POST request using HttpWebRequest?

22 April 2015 - 04:41 PM

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.


home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.