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.
Photo

Is it possible to send a POST request using HttpWebRequest?

http

Best Answer farmr, 23 April 2015 - 05:35 PM

Figured it out. It was not a pebkac after all.

I'm using a single file web server (mongoose), either by design or default configuration it sends a response as soon as it gets first TCP packet, and instead of waiting for the complete request to arrive - it sends a response. And client fails to receive it (for whatever reason, perhaps it resets inbound buffer after last packet is sent?).

 

Anyhow, it works as expected if I use a different web server (nancyfx, in my case).

Go to the full post


  • Please log in to reply
3 replies to this topic

#1 farmr

farmr

    New Member

  • Members
  • Pip
  • 2 posts

Posted 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.



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 April 2015 - 09:47 PM

Hi farmr,

You should be able to do this, no problem.

You will want to set the HttpWebRequest.Method, .ContentType and .ContentLength. Then use HttpWebRequest.GetRequestStream--and write your POST data, etc.

Here is a quick link to the NETMF documentation for HttpWebRequest:
https://msdn.microso...y/hh435250.aspx

Welcome to the Netduino community,

Chris

#3 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 23 April 2015 - 07:55 AM

Hi

 

Have a look at this post, towards the bottom it has some code which shows how to do a post

 

http://forums.netdui...-https-rest-url

 

@KiwiBryn

blog.devmobile.co.nz



#4 farmr

farmr

    New Member

  • Members
  • Pip
  • 2 posts

Posted 23 April 2015 - 05:35 PM   Best Answer

Figured it out. It was not a pebkac after all.

I'm using a single file web server (mongoose), either by design or default configuration it sends a response as soon as it gets first TCP packet, and instead of waiting for the complete request to arrive - it sends a response. And client fails to receive it (for whatever reason, perhaps it resets inbound buffer after last packet is sent?).

 

Anyhow, it works as expected if I use a different web server (nancyfx, in my case).







Also tagged with one or more of these keywords: http

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.