C# 如何将FormCollection对象发布到HTTP客户端

C# 如何将FormCollection对象发布到HTTP客户端,c#,asp.net-web-api,httpclient,dotnet-httpclient,C#,Asp.net Web Api,Httpclient,Dotnet Httpclient,我使用HTTP客户端调用RESTFul服务。现在我需要将FormCollection对象传递给API。该API不是REST API。有关API的更多信息,请参见此链接 我想用HTTPCLINET来实现这个。使用下面的代码,我能够得到响应 using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://testurl/");

我使用HTTP客户端调用RESTFul服务。现在我需要将FormCollection对象传递给API。该API不是REST API。有关API的更多信息,请参见此链接

我想用HTTPCLINET来实现这个。使用下面的代码,我能够得到响应

using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://testurl/");
                    var content = new FormUrlEncodedContent(new[]
                                                            {
                                                                new KeyValuePair<string, string>("Identification[api_key]", "somekey"),
                                                              new KeyValuePair<string, string>("Identification[InstallationID]", "installationid"),
                                                              new KeyValuePair<string, string>("action", "credit_application_link"),
                                                                new KeyValuePair<string, string>("Goods[Description]", "test"),
                                                                new KeyValuePair<string, string>("Identification[RetailerUniqueRef]", Guid.NewGuid().ToString()),
                                                                new KeyValuePair<string, string>("Goods[Price]", "100000"),
                                                                new KeyValuePair<string, string>("Finance[Code]", "PQERTS"),
                                                                new KeyValuePair<string, string>("Finance[Deposit]", "92000")
                                                            });
                var result = client.PostAsync("", content).Result;
                string resultContent = result.Content.ReadAsStringAsync().Result;
            }
我想知道,这种方法是否可行,或者是否有其他标准方法使用
httpclient
来实现


谢谢你的帮助。

看来你走对了方向;checkout Newtonsoft.Json——它是一个NuGet包,提供了使用Json的方法。特别是,您可以使用属性对属性进行注释,以控制包将对象序列化和反序列化为Json对象的方式

例如:

    [JsonObject(MemberSerialization.OptIn)]
    public class Person
    {
        // "John Smith"
        [JsonProperty]
        public string Name { get; set; }

        // "2000-12-15T22:11:03"
        [JsonProperty]
        public DateTime BirthDate { get; set; }

        // new Date(976918263055)
        [JsonProperty]
        [JsonConverter(typeof(JavaScriptDateTimeConverter))]
        public DateTime LastModified { get; set; }

        // not serialized because mode is opt-in
        public string Department { get; set; }
    }

您可以在上找到更多信息。

HttpClient只关心发送/接收原始内容;序列化留给其他库处理。我打算建议,它允许您使用其
PostUrlEncodedAsync
方法形成post对象,但它假设对象属性是简单类型(它只对值执行
ToString
)。您正在使用的序列化规则看起来相当自定义,因此我认为您一直在使用自己的东西。

谢谢您的回答。但问题似乎是,第三方(pay4later)不接受json类型。它必须是邮递的。我不确定。因为我已经尝试了你建议的选择。但他们总是说,请求无效。我明白了。。。我仔细查看了链接,发现他们要求表单输入。这有点超出了单个答案的范围,但基本上您需要创建自己的序列化方法来将对象转换为API要求的格式。
    [JsonObject(MemberSerialization.OptIn)]
    public class Person
    {
        // "John Smith"
        [JsonProperty]
        public string Name { get; set; }

        // "2000-12-15T22:11:03"
        [JsonProperty]
        public DateTime BirthDate { get; set; }

        // new Date(976918263055)
        [JsonProperty]
        [JsonConverter(typeof(JavaScriptDateTimeConverter))]
        public DateTime LastModified { get; set; }

        // not serialized because mode is opt-in
        public string Department { get; set; }
    }