【问题标题】:Passing a string value as a parameter HttpClient Post传递一个字符串值作为参数 HttpClient Post
【发布时间】:2020-05-02 13:58:17
【问题描述】:

我需要在帖子中将字符串值作为文本传递。

我已经尝试了以下

string content = "91237932,xy91856,0,0";
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(ConfigurationManager.AppSettings["AttributesApi"] + $"/api/UserProfiles/UpdateUserProfileFromIAM", new StringContent(content));

这是我必须使用的(不能按现有更改),但值为 null

[HttpPost("UpdateUserProfileFromIAM")]
public async Task<ActionResult> UpdateUserProfileFromIAM(string attributes)
{
    //attributes null
}

这个 HttpClient 帖子怎么做,让它是一个简单的字符串值?

【问题讨论】:

  • 取决于你在努力解决什么问题?如果它阅读结果,我已经发布了答案。如果不是,请澄清
  • 尝试将其放在根类中,例如:public class Root{public string attributes{get;set;}},并更改签名,例如:UpdateUserProfileFromIAM(Root root)

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


【解决方案1】:

如果您的问题是无法读取结果,请尝试以下操作:

public async Task<string> GetData() 
{
  string content = "91237932,xy91856,0,0";
  using var client = new HttpClient();
  var response = await client.PostAsync(ConfigurationManager.AppSettings["AttributesApi"] + $"/api/UserProfiles/UpdateUserProfileFromIAM", new StringContent(content));
  return await response.Content.ReadAsStringAsync();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-17
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多