gpt4 book ai didi

c# - 我想将数据从 c# 发送到 php

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:27 27 4
gpt4 key购买 nike

我想从 C# 向 PHP 发送一个字符串(数据),但我无法在 PHP 中接收数据。这是我的 C# 代码

    string deviceUrl ="http://localhost/CBIRS/clusteringdata.php";
HttpWebRequest reqDevice = (HttpWebRequest)WebRequest.Create(deviceUrl);
reqDevice.Method = "POST";
string deviceData = "status="+ data;
byte[] postDeviceBytes = Encoding.ASCII.GetBytes(deviceData);
reqDevice.ContentType = "application/x-www-form-urlencoded";
reqDevice.ContentLength = postDeviceBytes.Length;
Stream requestDeviceStream = reqDevice.GetRequestStream();
requestDeviceStream.Write(postDeviceBytes, 0, postDeviceBytes.Length);
requestDeviceStream.Close();

我的PHP代码如下。

   if(isset($_POST['status']))
{
$tempdata=explode('$',$_POST['status']);
var_dump($tempdata);
}
else
{
echo "Data not recieved";
}

在我运行 PHP 文件时运行 C# 应用程序后,它总是转到其他部分。我该如何纠正它?

最佳答案

您没有对 POST 数据进行编码。发送 POST 数据时,值必须经过 URL 编码。我怀疑 data 中有一个特殊字符导致 PHP 无法解析 POST 数据。

对值进行编码:

string deviceData = "status="+ HttpUtility.UrlEncode(data);

关于c# - 我想将数据从 c# 发送到 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22867969/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com