search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C# 傳送JSON給PHP API並等待PHP長時間運算並接收運算結果 – jashliao部落格

C# 傳送JSON給PHP API並等待PHP長時間運算並接收運算結果

 

GITHUB:https://github.com/jash-git/CS_wait_PHP

 

PHP

    header(‘content-type:text/html;charset=utf-8’);
    set_time_limit(0);//確保不會超時
    date_default_timezone_set(“Asia/Taipei”);//設定時區
    //echo “start….”.date (“Y- m – d / H : i : s”) .”
”;
    //echo “\n”;
    $data = file_get_contents(“php://input”);
    sleep(30);//函數延遲代碼執行若干秒。
    echo $data.”_”.date (“Y- m – d / H : i : s”);
    //echo “end….”.date (“Y- m – d / H : i : s”).”
”;
?>

 

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.IO;

namespace CS2PHPforJson
{

    class CS2PHPrestfulapi
    {
        public static String RESTfulAPI_get(String url)
        {
            //string url= “http://192.168.1.68:24410/syris/v8/controller”;
            String StrData = “”;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = “GET”;//request.Method = “POST”;
                //request.ContentType = “application/x-www-form-urlencoded”;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                string encoding = response.ContentEncoding;

                if (encoding == null || encoding.Length < 1)
                {
                    encoding = “UTF-8”; //預設編碼
                }

                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));

                StrData = reader.ReadToEnd();

                response.Close();
            }
            catch
            {
                StrData = “{\”Ans\”:\”Error\”}”;
            }
            return StrData;
        }

        public static String RESTfulAPI_postBody(String url, String data1)
        {
            String data;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = “POST”;

                //–
                //request.ContentType = “application/x-www-form-urlencoded”;//一般POST
                request.ContentType = “application/json; charset=UTF-8”;//POST to AJAX [is_ajax_request()]
                //request.Accept = “application/json, text/javascript”;//POST to AJAX [is_ajax_request()]
                //request.UserAgent = “”;//POST to AJAX [is_ajax_request()]
                //request.Headers.Add(“X-Requested-With”, “XMLHttpRequest”);//POST to AJAX [is_ajax_request()]
                //–

                request.ContentLength = data1.Length;
                StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
                writer.Write(data1);
                writer.Flush();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = “UTF-8”; //默认编码
                }

                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                data = reader.ReadToEnd();

                response.Close();
            }
            catch
            {
                data = “{\”Ans\”:\”Error\”}”;
            }

            return data;
        }

        public static String RESTfulAPI_putBody(String url, String data1)
        {
            String data;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = “PUT”;

                //–
                //request.ContentType = “application/x-www-form-urlencoded”;//一般POST
                request.ContentType = “application/json; charset=UTF-8”;//POST to AJAX [is_ajax_request()]
                //request.Accept = “application/json, text/javascript”;//POST to AJAX [is_ajax_request()]
                //request.UserAgent = “”;//POST to AJAX [is_ajax_request()]
                //request.Headers.Add(“X-Requested-With”, “XMLHttpRequest”);//POST to AJAX [is_ajax_request()]
                //–

                request.ContentLength = data1.Length;
                StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
                writer.Write(data1);
                writer.Flush();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = “UTF-8”; //默认编码
                }

                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                data = reader.ReadToEnd();

                response.Close();
            }
            catch
            {
                data = “{\”Ans\”:\”Error\”}”;
            }

            return data;
        }
    }

}
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS2PHPforJson
{
    class Program
    {
        static void Pause()
        {
            Console.Write(“Press any key to continue . . . “);
            Console.ReadKey(true);
        }
        static void Main(string[] args)
        {
            String StrData = “{\”firstName\”: \”Jash\”,\”lastName\”:\”Liao\”}”;
            String StrUrl = “http://localhost:8080/php_sleep.php”;
            Console.WriteLine(“C# send Data- ” + StrData);
            Console.WriteLine(“PHP url: ” + StrUrl);
            Console.WriteLine(“—————————————————————–“);
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(“C# start time- “+DateTime.Now.ToString(“yyyy/MM/dd HH:mm:ss”));
                Console.WriteLine(“\tphp: “+CS2PHPrestfulapi.RESTfulAPI_postBody(StrUrl, StrData));
                Console.WriteLine(“C# end time- ” + DateTime.Now.ToString(“yyyy/MM/dd HH:mm:ss”));
                Console.WriteLine(“—————————————————————–“);
            }
            Pause();
        }
    }
}

 

 

 

 

 



 




熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦