json c# - httpClient.postAsync在PHP回显服务器中返回空值,如何解决?

mi7gmzs6  于 2023-02-06  发布在  C#
关注(0)|答案(1)|浏览(203)

PostAsync在PHP echo服务器中返回null。我读了很多关于这个问题的帖子,没有一个能解决我的问题。我的一些C#端代码(.net 4. 8 vs 2022):

public class Person
    {
        public string Name { get; set; }
        public string Age { get; set; }

    }

    public static string url1 = "https://test.test/controller/app/test.php";
    

    static async Task<string> SendURI(Uri u, HttpContent c)
    {
        HttpClientHandler clientHandler = new HttpClientHandler();
        clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };

        var response = string.Empty;
        using (var client = new HttpClient(clientHandler))
        {
            HttpRequestMessage request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = u,
                Content = c
            };

            HttpResponseMessage result = await client.SendAsync(request);
            if (result.IsSuccessStatusCode)
            {
                //response = result.StatusCode.ToString();
                response = result.Content.ReadAsStringAsync().Result.ToString();
            }
        }
        return response;
    }

    private async void button5_Click(object sender, EventArgs e)
    {
        var person = new Person
        {
            Name = "John",
            Age = "34"
        };
        var data = System.Text.Json.JsonSerializer.Serialize(person);
        var dataS = new StringContent(data, Encoding.UTF8, "application/json");
        string res = await SendURI(new Uri(url1),dataS);
        MessageBox.Show(res);
    }

和php端(test.php php版本8.1):

$data = json_decode(file_get_contents('php://input'), true);
var_dump($data);

响应总是空的,这是什么问题?

ovfsdjhp

ovfsdjhp1#

感谢所有帮助过我的朋友。 Postman 的结果都很相似。我的问题很具体。我删除了。htaccess文件。带有rewriteRules的rewriteEngine重定向了我的url。

相关问题