Я хочу отправлять данные из C # (приложения Windows Forms) в PHP, используя $ _POST, но получая пустое значение при проверке с помощью echo / var_dump, а в окнах Locals VS, показывая значение ResponseStream с данными, которые были опубликованы , Я не знаю, если проблема в методе C # или PHP-скрипте или чем-то еще.
С Передача данных из C # в PHP , и еще несколько вопросов, в моем случае я применил их следующим образом.
PHP скрипт (post.php):
<code><?php
// *** Use $_GET, the echo works when testing parameters from browser's address bar, but GET method in C# will fail.
//if(isset($_POST['data'])){
//if($_SERVER['REQUEST_METHOD'] == "POST"){
$data = $_POST['data'];
echo "Hello, ";
echo $data;
//}
?>
<!-- for debug purpose -->
<pre>
<?php
var_dump($_POST);
//print_r($_POST);
var_dump($_REQUEST);
?>
C # метод:
public string SendPost(string url, string postData)
{
string webpageContent = string.Empty;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0x30 | 0xc0 | 0x300 | 0xc00);
// https://stackoverflow.com/questions/12506575/how-to-ignore-the-certificate-check-when-ssl
ServicePointManager.ServerCertificateValidationCallback +=
delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true; // **** Always accept
};
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
using (Stream webpageStream = webRequest.GetRequestStream())
{
webpageStream.Write(byteArray, 0, byteArray.Length);
}
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
webpageContent = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
MessageBox.Show("Data has been posted.");
return webpageContent;
}
Вот как я называю метод:
String pData = "TestString";
SendPost("https://websites/post.php", String.Format("data={0}", pData));
Результаты
В окнах Locals возвращаемое значение "webpageContent":
Hello, TestString
Из браузера "https://websites/post.php":
Hello,
Как сохранить / отобразить / использовать значение, полученное от C # в PHP?