c# - 使用 C# 调用 POST 请求调用。将 PowerShell 代码转换为 CSharp 代码
问题描述
如何将这一行 PowerShell 代码转换为 C# 代码
Invoke-RestMethod -Method Post -Uri ' https://s16events.azure-automation.net/webhooks?token=sdnfgknsdkfglkshnklsdfhgoihohsndfgndfgknkkdfg '
我正在使用 Visual Studio 来执行此操作
请告诉我
谢谢
解决方案
使用HttpClient
:
// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();
static async Task Main()
{
// Call asynchronous network methods in a try/catch block to handle exceptions
try
{
HttpResponseMessage response = await client.PostAsync("https://s16events.azure-automation.net/webhooks?token=sdnfgknsdkfglkshnklsdfhgoihohsndfgndfgknkkdfg", new StringContent(requestBody));
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch(HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ",e.Message);
}
}
示例改编自https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8
推荐阅读
- javascript - 使用nodejs将文件上传到s3
- spring-boot - 带有 Vault 的 Spring Cloud Config Server - Vault 覆盖本机配置文件中的任何值
- python - 将数据框的行保存到单独的 txt 文件中
- java - 无法理解如何在下面的课程中完成自动接线
- python - 如何解析两个粘在一起的元素?
- jasper-reports - 基于另一个字段的字段求和
- java - 将 Apache HttpClient 响应拦截器与请求匹配
- date - 我在 JSON 中保存了一个日期,当我重新加载它时,它看起来几乎一样......那是什么 `m=+2.58`?
- vue.js - Vuetify 删除 v-data-table 上的分页
- c# - 如何将 wacom 数位板集成到 WPF