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.

0

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Could you make and esample with Json or xml ? Thanks
@FabioRocco It depends on the data you want to retrieve, to generate the XML data use something in PHP like SimpleXMLElement and for reading it from C# use LINQ to XML and here's an example for using WebRequest. This should be enough to get you started.
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; }
    }
}

1 Comment

Thanks a Lot , i'm going to Try it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.