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

内森·埃尔金德

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

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

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

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

以下是网站有限文档的说明:

API 请求格式 我们的 REST API 基于开放标准,因此您可以使用任何 Web 开发语言来访问 API。

要发出请求,您将对适用的端点 URL 发出 HTTPS POST。

例如:https : //app.agencybloc.com/api/v1/individuals/search

URL 或查询字符串中不应包含任何参数。请求的正文必须包含安全凭证以及特定于方法的参数。

注意:来自 API 的响应采用方法描述中所述的 JSON 格式,但您不会发布 JSON 格式的请求。它们必须是上述的 application/x-www-form-urlencoded 格式。

这是我需要发布到网站的内容:

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 请求。我似乎找不到任何明确的文档,关于这个主题的其他问题对我来说都没有意义(作为 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;
        }

这是在 Button Click 上调用它的地方:

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;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用x-www-form-urlencoded强制Angular2进行POST

如何解决从openapi go代码生成器获取使用应用程序/ x-www-form-urlencoded内容类型的API的验证错误?

尝试使用json数据而不是纯文本或x-www-form-urlencoded进行JQuery发布时出现CORS问题

应用程序/ x-www-form-urlencoded还是multipart / form-data?

改造+ POST方法+ www-form-urlencoded

使用POST + application / x-www-form-urlencoded发送敏感数据

如何在Rails中使用x-www-form-urlencoded

尝试使用Content-Type应用程序/ x-www-form-urlencoded访问API

如何使用同构提取使用application / x-www-form-urlencoded标头和URLSearchParams进行POST

Swift-如何使用“ x-www-form-urlencoded”内容类型发送POST请求

Swift 4以x-www-form-urlencoded发送POST请求

对应用程序/ x-www-form-urlencoded请求类型传递@IsInt()验证

如何设置功能应用程序以从媒体类型“ application / x-www-form-urlencoded”提取数据

ASP.NET Core提取POST FormData()应用程序/ x-www-form-urlencoded

Spring MVC中内容类型应用程序/ x-www-form-urlencoded的请求参数的顺序

PHP应用程序/ x-www-form-urlencoded错误地解码了特殊字符

POST使用cURL和PHP中的x-www-form-urlencoded返回访问被拒绝

如何使用application / x-www-form-urlencoded创建API签名

存在Content-Type:application / x-www-form-urlencoded标头时,Grails RestfulController不使用JSON进行响应

Akka HTTP 如何使用 Content-Type application/x-www-form-urlencoded POST singleRequest

如何在内容类型 application/x-www-form-urlencoded 中使用自定义对象测试 Post 请求?

如何使用带有 loopj-async-http 库的 x-www-form-urlencoded 主体发送 post 请求

使用 HttpClient 从内容类型 application/x-www-form-urlencoded 使用 grant_type 和 scope POST

PowerShell - Slack API - 房间历史 - 带有 x-www-form-urlencoded 参数的 Post 方法

为什么 Volley String 请求不能在 x-www-form-urlencoded 中使用 POST 方法发送 Body 参数?

使用 application/x-www-form-urlencoded 格式在 java 应用程序中获取请求

使用 Jitterbit POST 应用程序/x-www-form-urlencoded Body 到 REST API

如何使用 FORM-DATA 或 x-www-form-urlencoded 与 Android 进行发布请求?

使用带有 x-www-form-urlencoded 的 HttpClient 的服务器端 Blazor Post