我需要创建一个供客户端使用的Web API“包装器”,但在这个Web API Service中,我实际上需要创建一个POST请求,该请求指向在同一个IIS服务器上运行的另一个REST API服务,该服务器执行一些工作并返回我通过JSON HttpResponse传递回客户端的StringContent。这个是可能的吗?客户端不会直接调用实际的REST API并返回他们不需要/不想要的数据,而是调用我的Web API服务,而我只返回所需的数据。我知道这是在旧的SOAP WSDL模型中完成的。
如果我需要客户端传递POST请求所需的几个参数,我会让客户端使用GET或POST请求吗?
发布于 2019-03-08 03:42:33
这是一个示例代码,我使用POST方法在另一个API中调用API。
using (var client = new HttpClient())
{
string query;
using (var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"username", username},
{"password", password}
}))
{
query = content.ReadAsStringAsync().Result;
}
var model = new{
username = txtUsername.Text,
password = txtPassword.Text
};
var json = JsonConvert.SerializeObject(model);
var user = new StringContent(json, Encoding.UTF8, "application/json");
using (var response = await client.PostAsync(@"http://localhost/dataagent/api/user/authenticate", user))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
// handle result here
}
}
}
https://stackoverflow.com/questions/55052489
复制相似问题