Main Site Documentation

Best way for accessing web pages - GetFromWeb is not working


#1

Hi.
I am back after a year. Had some seriously work in a job.
I starting to learn differences in 4.3.

I want to know what is the best approach to get web pages. I mostly want to post some data with GET parameters, or download files.

I try with this code:

        void SendValue(string Location, double temp, double hum)
        {
            if (!_IsTimeSet || !net.NetworkIsAvailable)
                return;
            string url = StringUtils.Format(ShareConst.HttpMicrocontrolerUrl + "PostMeasurements.php?location={0}", Location);
            if (!double.IsNaN(temp))
                url += "&temperature=" + temp;
            if (!double.IsNaN(hum))
                url += "&humidity=" + hum;
            HttpRequest request = WebClient.GetFromWeb(url);
            request.ResponseReceived += (HttpRequest sender, HttpResponse response) =>
                {
                    if (response.StatusCode == "200")
                    {
                    }
                    else
                    {
                        Debug.Print(StringUtils.Format("Measurement send: Location={1}, Error={2}", Location, response.Text));
                    }
                };
        }

The method is called every 15 seconds and it fails every second time, not matter how many seconds is between 2 requests.


#2

There are many options:

external library - https://netmftoolbox.codeplex.com/wikipage?title=Toolbox.NETMF.NET.HTTP_Client&referringTitle=Available%20classes




```cs]Gadgeteer.Networking.HttpHelper.CreateHttpGetRequest(url);[/code



```cs]Gadgeteer.Networking.WebClient.GetFromWeb(url);[/code


Codeshare - https://www.ghielectronics.com/community/codeshare/entry/126

How should I pick one? Does anybody know any problems for any approach? Which one if I want to post data to server? What if I want to use Authentication (Apache password protect directory)? What if I want to use SSL? Any problems with any when downloading large files (a few MB)?

#3

This code fails every second time:


Debug.Print("");
Debug.Print("");
Debug.Print("");
Debug.Print("Sending.");
var content = POSTContent.CreateTextBasedContent("testdata=12");
HttpRequest request = HttpHelper.CreateHttpPostRequest("http://192.168.1.27/Post.php", content, "application/x-www-form-urlencoded");
request.ResponseReceived += (HttpRequest sender, HttpResponse response) =>
{
    Debug.Print(response.Text);
};
request.SendRequest();

Output:

[quote]Sending.
Failed allocation for 343 blocks, 4116 bytes

The thread ‘’ (0x7) has exited with code 0 (0x0).
OK
The thread ‘’ (0x8) has exited with code 0 (0x0).

Sending.
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in System.Http.dll
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll

An exception occured while connecting to the Internet. Please, make sure that a valid URL is used and a network connection is up.

The thread ‘’ (0x9) has exited with code 0 (0x0).

Sending.
A first chance exception of type ‘System.ObjectDisposedException’ occurred in System.dll
Failed allocation for 343 blocks, 4116 bytes

The thread ‘’ (0xa) has exited with code 0 (0x0).
OK
The thread ‘’ (0xc) has exited with code 0 (0x0).

Sending.
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in System.Http.dll
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll

An exception occured while connecting to the Internet. Please, make sure that a valid URL is used and a network connection is up.

The thread ‘’ (0xd) has exited with code 0 (0x0).
The program ‘[17] Micro Framework application: Managed’ has exited with code 0 (0x0).[/quote]

I am using Cerberus, ENC28, NETMF and Gadgeteer Package 2014 R3.


#4

Didn’t know that. Thanks.


#5

Does anybody have any idea, why 2 different approaches failed every second time.


#6

@ Makla - I don’t use gadgeteer webrequest class, but with System.Net you can do something like this:


        string baseuri = "http://192.168.20.31:54986/ADCPostData.ashx?pluto=96969";

        public void SendRequestData()
        {
            WebRequest wreq = HttpWebRequest.Create(baseuri);
            HttpWebResponse wresp;
            wreq.Timeout = 2000;
            wreq.ContentType = "text/plain";
            wreq.Method = "POST";
            byte[] iobuff;
            string htmlcode = "Test send in post mode !";
            wreq.ContentLength = htmlcode.Length;
            using (Stream s = wreq.GetRequestStream())
            {
                iobuff = Encoding.UTF8.GetBytes(htmlcode);
                s.Write(iobuff, 0, iobuff.Length);
                s.Flush();
            }
            wresp = (HttpWebResponse) wreq.GetResponse();
            using (StreamReader s = new StreamReader(wresp.GetResponseStream()) )
            {
                Debug.Print(s.ReadToEnd());
            }
            wresp.Close();            
        }


Just for side note, I mostly use ASP.NET project to receive calls from netmf, specifically I create a “generic handler” project. I don’t know how to use Apache.
As told by andre.m, I gave up with cerberus board.