【问题标题】:Xamarin.Forms Send Data To API as application/x-www-form-urlencodedXamarin.Forms 将数据作为 application/x-www-form-urlencoded 发送到 API
【发布时间】:2021-05-26 08:24:09
【问题描述】:

我想将数据作为 x-www-form-urlencoded 而不是 json 发送到 api。我通常使用 newtonsoft.json nuget 包将数据作为 json 发送,我搜索但没有找到任何解决方案。提前致谢

【问题讨论】:

  • 尝试使用Refit
  • @cahyo 我会搜索更多,如果没有任何效果,我会尝试你的解决方案。谢谢你

标签: c# xamarin xamarin.forms x-www-form-urlencoded


【解决方案1】:

您可以发送字符串数据并将内容类型设置为x-www-form-urlencoded

using(var content=new StringContent())
{
    content.Headers.Remove("Content-Type");
    content.Headers.Add("Content-Type","x-www-form-urlencoded");
}

如果您的参数是用户名和密码,您可以这样做:

string param="username=admin&password=123456";
var content=new StringContent(param,Encoding.UTF8,"x-www-form-urlencoded");
var client=new HttpClient();
client.BaseAddress="https://localhost";
await client.PostAsync("/api/login",content);

【讨论】:

  • 如何发送参数?假设我有一个带有用户名和密码的类。我如何发送它们?
  • 如果你能发布一个例子,我会很感激的。
猜你喜欢
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2012-06-30
相关资源
最近更新 更多