gpt4 book ai didi

c# - 使用 Webclient 上传字符串将 JSON 字符串上传到 PHP

转载 作者:行者123 更新时间:2023-12-05 07:59:05 28 4
gpt4 key购买 nike

下面是我想实现与 PHP 服务器通信的代码:不幸的是,没有任何内容发布到 php 端(数组或单个字符串)。 var_dump 返回 null

**** 添加到 php 以读取 RAW 发布数据:**现在可以使用了

$json = file_get_contents('php://input');
$obj = json_decode($json);**

代码

   private void webClient2_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
MessageBox.Show(e.Result);
MessageBox.Show("ss");
}
private void btn_soksok_Click(object sender, RoutedEventArgs e)
{

StudentName student2 = new StudentName
{
f = "Craig",
l = "Playstead",
};


// string datax = JsonConvert.SerializeObject(student2);
string datax = "asdf";
Uri uri = new Uri("http://XXX.co/up.php");
WebClient webClient2 = new WebClient();
//webClient2.DownloadStringCompleted += webClient_DownloadStringCompleted;
webClient2.UploadStringCompleted += webClient2_UploadStringCompleted;

// webClient2.Headers["Content-Type"] = "application/json; charset=utf-8";
//webClient2.Headers["Accept"] = "application/json";


webClient2.UploadStringAsync(uri, "POST", datax);
MessageBox.Show(datax);

}

类对象

   public class StudentName {
public string f { get; set; }
public string l { get; set; }


}

和 php 端

<?php
//header('Content-Type: application/json;charset=utf-8');
var_dump($_POST);
$x = json_decode($_POST[0]);
echo $x;
?>

最佳答案

这就是你在代码中所需要的

webClient2.Headers["Content-Type"] = "application/x-www-form-urlencoded";

然后

 string datax = JsonConvert.SerializeObject(r);
datax = "0=" + datax;

在服务器中,我会将 echo $x; 替换为 print_r($x);

关于c# - 使用 Webclient 上传字符串将 JSON 字符串上传到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23093052/

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