簡體   English   中英

HttpClient Post 請求未在 C# 中發送 post 參數

[英]HttpClient Post request not sending post parameters in C#

我正在我的控制台應用程序中發送一個發布請求,但它似乎無法發送發布參數。 這是我的代碼

{
    string pass_url = "https://staging.fooddesk.be/api/v1/add_new_partner_pws";
    string postString = "[{\n\"product_name\": \"kip absrert\",\n\"producer_name\": \"Imperial Meat Products\",\n\"producer_art_num\": \"123\",\n\"supplier_name\": \"Vagro\",\n\"supplier_art_num\": \"12345\",\n\"ean_code\": \"5411153011327\",\n\"partner_art_nbr\": \"12345\"\n}]\"";
    var multipartContent = new MultipartFormDataContent();
    multipartContent.Add(new StringContent("pws_data" ) , postString) ;
    Uri requestUri = new Uri(pass_url);
    using (var objClint = new System.Net.Http.HttpClient())
    {
        objClint.DefaultRequestHeaders.Add("X-API-KEY", "ABCD");
        objClint.DefaultRequestHeaders.Add("X-COMP-KEY", "XYZ");
        System.Net.Http.HttpResponseMessage response = objClint.PostAsync(requestUri, multipartContent).Result;
        Console.WriteLine(response);
        if (response.IsSuccessStatusCode)
        {
            string json = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(json);
        }
    }
}

我在第 4 行遇到異常。請告訴我應該如何在 http 客戶端發布請求中發送發布數據。 提前致謝。

我相信你在第 4 行混淆了你的 arguments。

查看文檔以了解用法。

嘗試

multipartContent.Add(new StringContent(postString) , "pws_data") ;

此外,帖子字符串在 ] 之后的末尾有一個額外的引號。 刪除它應該會有所幫助。

[{“product_name”:“kip absrert”,“producer_name”:“Imperial Meat Products”,“producer_art_num”:“123”,“supplier_name”:“Vagro”,“supplier_art_num”:“12345”,“ean_code”:“ 5411153011327", "partner_art_nbr": "12345" }]"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM