I used socketclient sample from .Net Microframework SDK to call a web service. There is a better way I'm sure, but I'm newbie and I wanted to port my Arduino code fast.
It works.
Regards,
Mark
using System; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; using Socket = System.Net.Sockets.Socket; namespace NetduinoPlus_Temperature_Logger { public class Program { static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); static bool pressing = false; static string httpserver = "10.146.104.211"; static Int32 httpport = 81; static string SensorID = "000000000000000000000000000000000001"; public static void Main() { InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); button.OnInterrupt += new NativeEventHandler(button_OnInterrupt); while (true) { if (!pressing) { led.Write(true); Thread.Sleep(250); led.Write(false); Thread.Sleep(250); } } } static void button_OnInterrupt(uint data1, uint data2, DateTime time) { pressing = data2 == 0; String request = "POST /SensorService.asmx HTTP/1.1\n"; request += "Host: " + httpserver + "\n"; request += "Content-Type: text/xml; charset=utf-8\n"; request += "Content-Length: 418\n"; request += "SOAPAction: \"http://" + httpserver + "/StoreData\"\n\n"; request += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; request += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"; request += "<soap:Body>\n"; request += "<StoreData xmlns=\"http://" + httpserver + "/\">\n"; request += "<password>upstempdata</password>\n"; request += "<Value>"; request += "22.22"; request += "</Value>\n"; request += "<SensorID>"; request += SensorID; request += "</SensorID>\n"; request += "</StoreData>\n"; request += "</soap:Body>\n"; request += "</soap:Envelope>\n\n"; try { String html = Post(httpserver, httpport, request); Debug.Print(html); } catch (SocketException se) { Debug.Print("SocketException when connecting to " + httpserver + ":" + httpport); Debug.Print("If your network uses IPSec, you may need enable the port manually"); Debug.Print("Socket Error Code: " + se.ErrorCode.ToString()); Debug.Print(se.ToString()); } } /// <summary> /// Issues a http POST request on the specified server. /// </summary> /// <param name="server"></param> /// <param name="port"></param> /// <param name="request"></param> /// <returns></returns> private static String Post(String server, Int32 port, String request) { const Int32 c_microsecondsPerSecond = 1000000; // Create a socket connection to the specified server and port. using (Socket serverSocket = ConnectSocket(server, port)) { // Send request to the server. Byte[] bytesToSend = Encoding.UTF8.GetBytes(request); serverSocket.Send(bytesToSend, bytesToSend.Length, 0); // Reusable buffer for receiving chunks of the document. Byte[] buffer = new Byte[1024]; // Accumulates the received page as it is built from the buffer. String page = String.Empty; // Wait up to 30 seconds for initial data to be available. Throws an exception if the connection is closed with no data sent. DateTime timeoutAt = DateTime.Now.AddSeconds(30); while (serverSocket.Available == 0 && DateTime.Now < timeoutAt) { System.Threading.Thread.Sleep(100); } // Poll for data until 30-second timeout. Returns true for data and connection closed. while (serverSocket.Poll(30 * c_microsecondsPerSecond, SelectMode.SelectRead)) { // If there are 0 bytes in the buffer, then the connection is closed, or we have timed out. if (serverSocket.Available == 0) break; // Zero all bytes in the re-usable buffer. Array.Clear(buffer, 0, buffer.Length); // Read a buffer-sized HTML chunk. Int32 bytesRead = serverSocket.Receive(buffer); // Append the chunk to the string. page = page + new String(Encoding.UTF8.GetChars(buffer)); } // Return the complete string. return page; } } /// <summary> /// Creates a socket and uses the socket to connect to the server's IP address and port. /// </summary> /// <param name="server"></param> /// <param name="port"></param> /// <returns></returns> private static Socket ConnectSocket(String server, Int32 port) { // Get server's IP address. IPHostEntry hostEntry = Dns.GetHostEntry(server); // Create socket and connect to the server's IP address and port Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(new IPEndPoint(hostEntry.AddressList[0], port)); return socket; } } }
- TechnoGuy likes this