如何从 .Net Core 3.1 代码调用 cURL 命令

How to I make a cURL command call from .Net Core 3.1 Code

我有一个复杂的 cURL 命令,它工作得很好:

curl -L --negotiate -u : -b ~/cookiejar.txt  "https://idp.domain.net/oauth2/authorize? scope=openid&response_type=code&redirect_uri=https://localhost:5001&client_id=client_id_here"

然而,使用 HttpClient 在 C# 中进行复制并不可行。但 我需要从 .Net Core 代码调用 cURL 命令并获取响应。

有没有办法在 .Net Core (v3.1) 中进行正常的 cURL 命令调用?

(如果您有兴趣,这是 cURL 命令的详细信息:

这可以通过集成调用 powershell 来完成。

  1. 安装 NuGet Microsoft.Powershell.SDK。
  2. 添加System.Management.Automation
  3. using语句
  4. 添加以下方法:

-

public List<string> ExecutePowershell(string command)
{
    var resultsAsString = new List<string>();
    using (var ps = PowerShell.Create())
    {
        var results = ps.AddScript(command).Invoke();
        foreach (var result in results)
        {
            resultsAsString.Add(result.ToString());
        }
    }
    return resultsAsString;
}
  1. 这样称呼它:

-

void Main()
{
    var results = ExecutePowershell("curl -L --negotiate -u : -b ~/cookiejar.txt  /"https://idp.domain.net/oauth2/authorize? scope=openid&response_type=code&redirect_uri=https://localhost:5001&client_id=client_id_here/"");
    Console.WriteLine(results);
}

我从这个答案中得出这个: