【问题标题】:Call POST request call using C#. Convert PowerShell code to CSharp code使用 C# 调用 POST 请求调用。将 PowerShell 代码转换为 CSharp 代码
【发布时间】:2019-11-02 13:29:50
【问题描述】:

如何将这一行 PowerShell 代码转换为 C# 代码

Invoke-RestMethod -Method Post -Uri 'https://s16events.azure-automation.net/webhooks?token=sdnfgknsdkfglkshnklsdfhgoihohsndfgndfgknkkdfg'

我正在使用 Visual Studio 来执行此操作

请告诉我

谢谢

【问题讨论】:

    标签: c# visual-studio powershell


    【解决方案1】:

    使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      相关资源
      最近更新 更多