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