【问题标题】:How to post FormCollection object to HTTP Client如何将 FormCollection 对象发布到 HTTP 客户端
【发布时间】:2016-03-17 07:21:20
【问题描述】:

我使用 HTTP 客户端来调用 RESTFul 服务。现在我有一个要求,我必须将 FormCollection 对象传递给 API。 API 不是 REST API。有关 API 的更多信息,您可以在此链接中查看。 http://docs.pay4later.com/docs/requests

我想使用 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 发送该模型。但这并不成功,因为请求是作为 json 对象进行的。模型如下图,

public class CreditApplicationInitializationRequest
    {
        public string action { get; set; }

        public Identification Identification { get; set; }

        public Goods Goods { get; set; }

        public Finance Finance { get; set; }
    }
public class Identification
    {
        public string api_key { get; set; }

        public string RetailerUniqueRef { get; set; }

        public string InstallationID { get; set; }
    }

我想知道,这种方法是否可行,或者是否有任何其他标准方法使用httpclient 来做到这一点。

感谢您的帮助。

【问题讨论】:

    标签: c# asp.net-web-api httpclient dotnet-httpclient


    【解决方案1】:

    看起来你在正确的轨道上;结帐 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; }
        }
    

    您可以在http://www.newtonsoft.com/json/help/html/SerializationAttributes.htm找到更多信息。

    【讨论】:

    • 感谢您的回答。但问题似乎是,第三方(pay4later)不会接受 json 类型。它必须是表格帖子。我不知道。因为我已经尝试了你所建议的选项。但他们总是说,无效的请求。
    • 我明白了...我仔细查看了链接,可以看到他们要求输入表单。这有点超出了单一答案的范围,但基本上你需要创建自己的序列化方法来将你的对象转换为 API 要求的格式。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-25
      • 2021-05-31
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 2018-02-13
      相关资源
      最近更新 更多