我该如何做一些简单的事情:
curl -X POST --data-binary '{"a":"b"}' --location "$URL"
我正在使用 Xamarin、c#
我遇到依赖问题。
最佳答案
您可以通过添加对 System.Net.Http
的系统引用来使用 HttpClient
。使用以下示例代码创建客户端并提交相同的调用。请注意,您可以使用另一个框架将类类型转换为 JSON 文本,例如 Json.NET。请注意,以下示例显示了如何构造 HttpClient
,但您应该not dispose of the instance and track it elsewhere .
var client = new HttpClient()
string content = "{\"a\":\"b\"}";
StringContent httpContent = new StringContent(content);
var response = await client.PostAsync("$URL", httpContent);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
// Show response.
}
else
{
// Show error.
}
关于c# - curl 后 Xamarin C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36993447/