c# - curl 后 Xamarin C#

标签 c# post curl xamarin

我该如何做一些简单的事情:

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/

相关文章:

c# - VB.NET 应用程序中的脚本自动化

c# - NHibernate.Exceptions.GenericADOException 为什么?

c# - 在带有 MVVM 的 DataTemplate 中使用窗口资源

ios发送带有POST参数的POST请求

JAVA POST 请求带有文件和参数的 MultiFormData

curl - 通过 API 导出 Grafana 仪表板

c# - 使属性在 DataGridView 中可见但在 PropertyGrid 中不可见?

PHP 单选按钮获取两个值

php - Guzzle (35) gnutls_handshake() 失败 : An unexpected TLS packet was received over HTTP

python - API:Python中使用curl调用URL