尝试在 wpf 桌面应用程序中使用 application/x-www-form-urlencoded 创建 API POST

Trying to make an API POST using application/x-www-form-urlencoded in a wpf desktop app

我正在为工作创建 WPF 桌面客户端,需要使用它从网站获取数据 API。

此网站 API 需要 POST 请求才能获取数据,而不是 GET。据我所知,这是一项安全功能。

我不知道如何格式化我的请求,因为大多数文档都是针对 application/json 请求的,但我需要 POST 我的请求使用 application/x-www-form-urlencoded .

然后 API 将以 JSON 格式响应我需要解析和使用的请求数据。

以下是网站有限文档的内容:

API Request Format Our REST API is based on open standards, so you can use any web development language to access the API.

To make a request, you'll make an HTTPS POST to the applicable endpoint URL.

For example: https://app.agencybloc.com/api/v1/individuals/search

No parameters should be in the URL or querystring. The body of the request must contain the security credentials, as well as the method-specific parameters.

Note: The responses from the API are in JSON format as described in the method descriptions, but you do not POST JSON-formatted requests. They must be in application/x-www-form-urlencoded format per the above.

这是我需要 POST 到站点的内容:

Host: https://app.agencybloc.com/api/v1/individuals/search
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

sid=mySID&key=myKey&updatedDTM=09/20/2021

这是我到目前为止的代码。

    public class tdate
    {
        public string updatedDTM { get; set; }
    }

    public class APIHelper
    {

        public void Main(string[] args)
        {

            HttpClient client = new HttpClient();

            HttpContent content = new FormUrlEncodedContent(
                new List<KeyValuePair<string, string>>()
                );

            content.Headers.ContentType = new MediaTypeHeaderValue("application/x--www-form-urlencoded");
            content.Headers.ContentType.CharSet = "UTF-8";
            content = "sid=mySID&key=myKey&updatedDTM=09/20/2021";
            client.DefaultRequestHeaders.ExpectContinue = false;
            client.BaseAddress = new Uri(https://app.agencybloc.com/api/v1/individuals/search/); 

        }
    }

我觉得我离这里很远。我不知道如何正确完成 POST 请求。我似乎找不到任何明确的文档,关于这个主题的其他问题 none 对我来说很有意义(作为 C# 中的相对新手)。

对于项目的其余部分,我将不得不解析 JSON 响应并使用该数据发出第二个 POST 请求以获取更多数据。

如果我需要提供更多代码或任何其他详细信息,请告诉我。

感谢您的帮助。

编辑: 让这个工作。

方法如下:

public static HttpResponseMessage BlocCall()
        {

            HttpClient client = new HttpClient();

            var dict = new Dictionary<string, string>();
            dict.Add("sid", "MyID");
            dict.Add("key", "MyKey");
            dict.Add("lastName", "Heine");

            var req = new HttpRequestMessage(HttpMethod.Post, "https://app.agencybloc.com/api/v1/individuals/search/") { Content = new FormUrlEncodedContent(dict) };
            var res = client.SendAsync(req).Result;
            
            Newtonsoft.Json.JsonConvert.SerializeObject(res, Formatting.Indented);
            Trace.WriteLine(res);
            return res;
        }

这里是点击按钮时调用它的地方:

private void Button_Click(object sender, RoutedEventArgs e)
        {
            APIHelper.BlocCall();
      
        }

你可以检查类似的内容。

        HttpClient client = new HttpClient();

        var dict = new Dictionary<string, string>();
        dict.Add("sid", "mySID");
        dict.Add("key", "myKey");
        dict.Add("updatedDTM", "09/20/2021");

        var req = new HttpRequestMessage(HttpMethod.Post, "https://app.agencybloc.com/api/v1/individuals/search/") { Content = new FormUrlEncodedContent(dict) };
        var res =  client.SendAsync(req).Result;