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

Sending data to php website

http php website request post

Best Answer Dave1374, 12 August 2013 - 12:17 PM

Hey VanKurt.

 

This is how I send data to my wcf service. 

string query = "http://www.x.com/Service1.svc/Log?Log=" + data;using (HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(query)){    myReq.Method = "GET";    using (HttpWebResponse WebResp = (HttpWebResponse)myReq.GetResponse())    {          }}

Both objects require  

using System.Net;

Maybe I'm doing it wrong tho... didn't know the netmf toolbox had a httpclient class.

 

You should had try catch to the code snippet btw.

 

here's an interesting stackoverflow topic about the using statement. not to be confused with the one to access a namespace.

 

edit: heh just realised you already had usings in your code... so, yeah.

 

Dave

Go to the full post


  • Please log in to reply
4 replies to this topic

#1 VanKurt

VanKurt

    Member

  • Members
  • PipPip
  • 19 posts

Posted 10 August 2013 - 01:38 PM

Hi there!

I'm currently trying to send data from the netduino plus to my webserver via a special php site. When I call the site manually from my webbrowser everything works fine. But when doing the same via code from the netduino it does not work.

 

Here's the sending code (inspired by these forums):

private void SendHttpRequest(string serverUrl, string request){  IPHostEntry host = Dns.GetHostEntry(serverUrl);  IPEndPoint endPoint = new IPEndPoint(host.AddressList[0], 80);  using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))  {    socket.Connect(endPoint);    using (NetworkStream ns = new NetworkStream(socket))    {      byte[] bytes = System.Text.Encoding.UTF8.GetBytes("GET " + request + " HTTP/1.1rn");      ns.Write(bytes, 0, bytes.Length);    }  }}

And this is how I call it:

SendHttpRequest("www.mywebsite.com", "/netduino/submitData.php?data=5");

The problem is, that the php script on the website is never called. So I guess there is something wrong with the way I send the HTTP request?

 

Thanks for your help!



#2 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 11 August 2013 - 12:43 PM

Is this PHP script hosted on a shared webserver (i.e. cheap hosting account)?  If so then it will be setup using HostNames for multiple site support from a single IP.  As you are connecting directly to the server by IP you need to specify the hostname for your site as a HTTP header.

 

If on the other hand the script is hosted locally, try just specifying the IP address of the server and check that you can access your site just by its IP.  As unless the IP is bound to port 80 as well as a host name your server wont respond.

 

Finally check that the name is being resolved correctly by the Dns call, sometimes i have found the ND to need the DNS server to be specified manually as it wasnt for some reason being assigned by DHCP.

 

I would also try and use the HttpClient in the netmf toolbox, https://netmftoolbox...ailable classes as it works very well and lets you post data easily as well...

 

You could also look at the source for inspiration as well.

 

Nak.



#3 Dave1374

Dave1374

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts

Posted 12 August 2013 - 12:17 PM   Best Answer

Hey VanKurt.

 

This is how I send data to my wcf service. 

string query = "http://www.x.com/Service1.svc/Log?Log=" + data;using (HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(query)){    myReq.Method = "GET";    using (HttpWebResponse WebResp = (HttpWebResponse)myReq.GetResponse())    {          }}

Both objects require  

using System.Net;

Maybe I'm doing it wrong tho... didn't know the netmf toolbox had a httpclient class.

 

You should had try catch to the code snippet btw.

 

here's an interesting stackoverflow topic about the using statement. not to be confused with the one to access a namespace.

 

edit: heh just realised you already had usings in your code... so, yeah.

 

Dave



#4 VanKurt

VanKurt

    Member

  • Members
  • PipPip
  • 19 posts

Posted 13 August 2013 - 06:02 AM

Allright, got it working  :D

 

I ended up including the System.Http assembly, which makes everything nice and easy. And it adds only 30kB to the final application size.



#5 markleeapps

markleeapps

    New Member

  • Members
  • Pip
  • 1 posts

Posted 04 September 2013 - 10:19 PM

VanKurt,

You were trying to send data to your php website, and you got it working with System.Http. Can you please post the code for the Netduino and also the php code.

Thanks,

Mark







Also tagged with one or more of these keywords: http, php, website, request, post

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.