1

I would retrive the Return value from a function php to a C# WinApp Client.

I have some functions in a php page. Those functions use Get Method and elaborate some data recived from a C# Winapp Client.

So php pages after elaborate the data return a value. Now i would get this value from C# client.

I would do this 'on fly' without save any files on the Pc Client.

how can I do this?

Edit : If someone Could make an exampe with Json or XML i will appreciate it.

2 Answers2

1

You need to create a PHP script on the server that retrieve the data you need then return the results as SOAP, XML or JSON then you can request that page from you C# application using WebRequest (MSDN link).

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
  • Could you make and esample with Json or xml ? Thanks – Fabio Rocco Nov 12 '11 at 17:15
  • @FabioRocco It depends on the data you want to retrieve, to generate the XML data use something in PHP like [SimpleXMLElement](http://stackoverflow.com/questions/486757/how-to-generate-xml-file-dynamically-using-php) and for reading it from C# use [LINQ to XML](http://msdn.microsoft.com/en-us/library/bb387098.aspx) and [here's](http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx) an example for using WebRequest. This should be enough to get you started. – Nasreddine Nov 12 '11 at 17:40
0

A simple example with JSON output:

PHP code:

<?php 
#header('content-type:application/json');
if(array_key_exists("foo", $_POST) && !empty($_POST["foo"])) {
   $data = array('foo' => 'baa', 'x' => 'y', 'sucess' => 'true', 'error' => 'null');
}
else {
    $data = array('error' => 'foo is empty', 'sucess' => 'false');
}

    die(json_encode($data));
?>

C#.NET code:

using System;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            string response = DoRequest();
            JavaScriptSerializer ser = new JavaScriptSerializer();
            View json = ser.Deserialize<View>(response);
            if (json.sucess)
            {
                // do something.. 
            }
            else
            {
                Console.WriteLine("Erro:{0}", json.error);
            }

        }

        static string DoRequest()
        {
            string domain = "..."; // your remote server 
            string post = "foo=baa";
            byte[] data = Encoding.ASCII.GetBytes(post);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(domain);
            request.Method = "POST";
            request.Referer = "desktop C# Application";
            request.ContentLength = data.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            Stream stream = request.GetRequestStream();
            stream.Write(data, 0, data.Length);

            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            char[] buffer = new char[256];
            int count;
            StringBuilder buf = new StringBuilder();
            while ((count = sr.Read(buffer, 0, 256)) > 0)
            {
                buf.Append(buffer, 0, count);
            }

            response.Close();
            stream.Close();
            sr.Close();

            return buf.ToString();

        }
    }

    public class View
    {
        public string foo { get; set; }
        public string x { get; set; }
        public bool sucess { get; set; }
        public string error { get; set; }
    }
}
Kakashi
  • 2,165
  • 14
  • 19