簡體   English   中英

C#創建HTTP POST Web請求

[英]c# creating HTTP POST web request

大家好,我在其中揮舞着網址http://somehost/swagger/index.html結束方法,如圖所示: 在此輸入圖像描述

我正在嘗試創建HTTP POST Web請求,該請求是post方法之一。

using System;
using System.Collections.Generic;
using System.Net.Http;
namespace SwaggerConsoleAPP
{
    class Program
    {  
        static  void Main(string[] args)
        {
            postRequest("http://somehost/api/Referral/GetReferralsByPersonalIdNumber");
                Console.ReadKey();
        }

        async  static void postRequest (string url){
            IEnumerable<KeyValuePair<string, string>> queries = new List<KeyValuePair<string, string>>() {
                new KeyValuePair<string, string>("1133221221","5642")

            };
            HttpContent q = new FormUrlEncodedContent(queries);
            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage responce = await client.PostAsync(url,q))
                 {
                    using (HttpContent content = responce.Content)
                    {
                        string mycontent = await content.ReadAsStringAsync();

                        Console.WriteLine(mycontent);
                    }
                 }
            }
        }
}
    }

這是控制台應用程序,但控制台寫入錯誤:

{“”:[“輸入無效。”]}

這是方法的模型 在此輸入圖像描述

有幫助嗎?

我認為,鍵值對是造成此問題的原因。

API希望將personalIDNumberpharmacyID作為兩個單獨的參數。

最簡單的解決方案是只創建具有以下兩個屬性的類:

public class InputFields
{
    public string personalIDNumber{get;set;}
    public string pharmacyId{get;set;}
}

只要您的API將此類型的模式作為輸入參數,此方法就起作用。

請注意,在HTTP正文中發送兩個參數作為發布,您可能還需要另外編碼,如此處所述。

希望這可以幫助。

暫無
暫無

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

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