【发布时间】: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