WebRequest.Create() does not handle column character in URL string

I am trying to post some real-time data to an OpenEnergyMonitor site, which requieres to have column characters in URL string. The API-omplient string look like this:

http://emoncms.org/api/post?apikey=MyKEY&json={p0:385}

This works fine with .NET framework for windows, but .NETFM throws an exception in attempts tpo parse URL:

“System.Convert::ToInt64\r\nSystem.Convert::ToInt32\r\nGHIElectronics.NETMF.Net.Uri::ParseUriString\r\nGHIElectronics.NETMF.Net.Uri::ConstructAbsoluteUri\r\nGHIElectronics.NETMF.Net.Uri::.ctor\r\nGHIElectronics.NETMF.Net.WebRequest::Create\r\nHpm.Emon::PostData\r\nHpm.Emon::PostDataLoop\r\n”

IS it a known pronblem wiht ht efrmawork?
Did anybody else found the way to post data to emoncsm?

Thanks for any suggestions,
Alex

do a search for json in codeshare…
you will find plenty of example there…

Welcome to the community!

Jay Jay,

Thank you for the suggestion. I will check some code on the codeshare. But the first attempt did not help me to find answer to my question.
As I understand correctly json libraries helps to serialize data in order to get it transferred over the web.
The problem I am facing is related to a specific API expecting the data to be embedded in the URL. This makes the URL string not parseble by .NETMF library.
I tried to deliver the data as the request content, but the target server does not seem to be accepting it in this form - only as a part of URL string.

Alex

Hi Alex, its going to be best if you paste some code so we can see. tell us what the URL is meant to look like and how the post process is meant to occur, and we can look at it from the code perspective. I think there’s no reason that this won’t work on netmf.

Brett,

The API for posting data. It is presented on emoncms.org site as an example:

http://www.emoncms.org/api/post?apikey=SOMEKEY&json={power:252.4,temperature:15.4}

As you can see the URL for posting data will always have column chracters when data point values are serialized.
The following is a simplified fragment of my code that fails on WebRequest.Create():

            
            string urlStr;
            byte[] bs;
            using (MemoryStream s = new MemoryStream())
            {
                Utf8Stream.Write("http://emoncms.org/api/post?apikey=", s);
                Utf8Stream.Write(c_ApiKey, s);
                Utf8Stream.Write("&json={", s);
                WriteDataPoints(s, isPower);  // local function building point value elelment like: power:252.4,temperature:15.4
                Utf8Stream.Write("}", s);
                bs = s.ToArray();
            }
            char[] chs = System.Text.Encoding.UTF8.GetChars(bs);
            urlStr = new string(chs);
          var request = (HttpWebRequest)WebRequest.Create(urlStr);

This builds urlStr similar to th eexample above

If I replaced ‘:’ with any any character in URL string, Create() function succee, but of cause the json serialisation object become invalid.

Alex

First up, is there any reason you’re doing the UTF8Stream ? To me, the only thing you need to do is to create a string object, then UTF8 it; the text you show a few posts ago really seems strange.

You didn’t mention which board you are using and if you are using a Gadgeteer board like the spider try this code:
customize myPostContent with your own API key and data you want to sent…
i tried it and got a response back from the server.


        void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/
            ethernet_J11D.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_J11D_NetworkUp);
            ethernet_J11D.UseDHCP();

            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }
    void ethernet_J11D_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            var myPostContent = POSTContent.CreateTextBasedContent("SOMEKEY&json={power:252.4,temperature:15.4}");
            
            var myReq =
                HttpHelper.CreateHttpPostRequest("http://www.emoncms.org/api/post?apikey=",
                myPostContent, "application/json");

            myReq.ResponseReceived += new HttpRequest.ResponseHandler(myReq_ResponseReceived);
            myReq.SendRequest();
        }

        void myReq_ResponseReceived(HttpRequest sender, HttpResponse response)
        {
             if (response.StatusCode != "200")
                Debug.Print(response.StatusCode);
            Debug.Print("Success \n" + response.Text);

        }


cheers.

The board is Panda II.
Yes, I did try to deliver data throught the request content. Server responded 200-OK, but it did not accepted the data.
I used slightly different method (s cantains API key and jason-serilized data):


            request.ContentType = "text/plain";
            request.ContentLength = s.Length;
            using (Stream os = request.GetRequestStream())
            {
                    s.WriteTo(os);
            }

but I guess it was producing the same result as in your example.

I also asked for help on openenergymonitor (host of emoncms) forum. I got a response that the server also supports a CSV encoding, but I did not find enough details to try it yet. I will post more details when I get them.
Alex

Alex, humour me and just make the string the URL that you think that you need, and do a POST with that. I honestly believe you’re over-working this, and this kind of approach just works. If you have a look at the code in the Cosm/Pachube example on the codeshare site, http://www.tinyclr.com/codeshare/entry/477 then you can see that the approach is very simple; piece together your URI with your data in it and then post to the server.

That is very possible, Brett :slight_smile:
I will have to check this out in a few days after coming back from the vacation.
Alex