C# 傳送JSON給PHP API並等待PHP長時間運算並接收運算結果
C# 傳送JSON給PHP API並等待PHP長時間運算並接收運算結果
GITHUB:https://github.com/jash-git/CS_wait_PHP
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”) .”<br>”; //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”).”<br>”; ?> |
C#
using System; namespace CS2PHPforJson class CS2PHPrestfulapi HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); StrData = reader.ReadToEnd(); response.Close(); public static String RESTfulAPI_postBody(String url, String data1) //–
request.ContentLength = data1.Length; StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
response.Close(); return data; public static String RESTfulAPI_putBody(String url, String data1) //–
request.ContentLength = data1.Length; StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
response.Close(); return data; } |
using System; namespace CS2PHPforJson |