Making post request with WebRequest stalled when requesting stream

Hello there.
I´m trying to post some data to a firebase rest service using WebRequest since there are no HttpClient for .NETMF as far as I know (correct me if I¨m wrong :slight_smile: )

I have this code as shown below in a normal .net 4.5 console application it works just fine and I get data stored in my firebase database but when I´m using .NETMF 4.3 it just stall.

Anyone have a solution for a workaround or another library for making HTTP requests.

WebRequest request = WebRequest.Create("https://******");

request.Method = "POST";
request.ContentType = "application/json";

string postData = Json.NETMF.JsonSerializer.SerializeObject(logObject, DateTimeFormat.Ajax);
byte[] byteArray = System.Text.UTF8Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
//----------- CODE STALLED AFTER THIS LINE ---------------
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Debug.Print(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Debug.Print(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();

I’ve been using code very similar to yours with ThingSpeak with no issues. The following logged about 900 data points once per minute before I turned the application off:

private static string SendSSL(string server, string data)
{
    string uri = "https://api.thingspeak.com/update";
    using (var request = (HttpWebRequest) WebRequest.Create(uri))
    {
        request.Headers.Add("X-THINGSPEAKAPIKEY: API KEY GOES HERE");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(UTF8Encoding.UTF8.GetBytes(data), 0, data.Length);
        }

        var response = (HttpWebResponse) request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
        Debug.Print("SSL Response:");
        Debug.Print(responseString);
        return(responseString);
    }
}

I have also used similar code to post to Sparkfun’s Phant service (now decommissioned) in the past.

Regards,
Mark

I will try your example out when ill get home to my workstation :slight_smile:

Just wondering what the difference is between the System.Net.WebRequest on .NET 4.5 and .NETMF 4.3 since it´s working on .NET 4.5 :confused:

Still just stalled on the same line.
have a feeling that this version os System.Http on this version is kina broken. :slight_smile:

Okay got it to work somehow.
When I push the onboard button I call my method that calls the WebRequest method.

When I trigger the button with InterruptPort it does not work, but if I just call in while loop that checks an InputPort it works.

So why is that?

Wonder if the interrupt is being generated on a separate thread…

Regards,
Mark

It might I´m a rookie on multithreads and all things regarding the Netduino :slight_smile:

Hmmmm, think I’ve just blown the multithreading hypothesis out of the Window. I’ve just had my ThingSpeak class running for 24 hours using a Timer and it’s worked fine.

Regards,
Mark

Hmm.
yet another question regards the WebRequest

I have 4 digital inputs that I’m looping over if they read true and it sometimes happens quite fast 3-4 times over a min or so
and every time it reads true I’m sending a request to an REST API.
But for some reason after the 3rd request I’m making I get an Exception and I have to restart the netduino for it to work again. Are there any max request in a period of time before I can send more?
I almost the same as your example for requesting.

@Thomas_Boeriis

But for some reason after the 3rd request I’m making I get an Exception and I have to restart the netduino for it to work again.

I experienced a similar issue when using Maple. I don’t see any exceptions though. But after 3 requests it stops working. Let me know if you figure anything out because it might be the same issue happening with Maple.

I have just completed a library to post to ThingSpeak and that was posting once a minute and I’ve had it running for over 24 hours using this method.

The one thing I did have to do was to wrap the call the the library in an exception handler as I did get the odd exception, about 3 over the 24 hour soak test period.Regards,
Mark

@Nevyn

I removed Maple from the equation and I don’t have the same issue yet. I suspect something in that library for now.

@john; if you find anything specific, file an issue; we’d love to harden it.

@bryancostanich

Already have: https://github.com/WildernessLabs/Maple/issues/2

:slight_smile:

I may have found the underlying issue I was experiencing. See my latest comment on the Maple issue.

Are there any current plans to update the firmware for .NETMF 4.4?
Is there a way I can do this today?

1 Like

I think the problem lies in the routine WriteOutputStream of class RequestHandlerBase in Maple.
I copied the Maple source code and debugged it locally. I then found it stopped giving me the problem as soon as I removed the enclosing “using (this.Context.Response.OutputStream)” in WriteOutputStream. The exception trace given earlier in this thread lead me in this direction.
Hope this helps.

1 Like