【问题标题】:How to I make a cURL command call from .Net Core 3.1 Code如何从 .Net Core 3.1 代码进行 cURL 命令调用
【发布时间】:2020-07-14 21:30:35
【问题描述】:
我有这个复杂的 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"
但是,在 C# 中使用 HttpClient 进行复制实际上并不可行。但
我需要从 .Net Core 代码中调用 cURL 命令并获取响应。
有没有办法在 .Net Core (v3.1) 中进行正常的 cURL 命令调用?
(如果您有兴趣,这是 cURL 命令的详细信息:Replicate cURL Command Using Redirect and Cookies in .Net Core 3.1)
【问题讨论】:
标签:
c#
powershell
curl
.net-core
.net-core-3.1
【解决方案1】:
这可以通过对 powershell 的集成调用来完成。
- 安装 NuGet Microsoft.Powershell.SDK。
- 添加
System.Management.Automation的using语句
- 添加如下方法:
-
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;
}
- 这样称呼它:
-
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);
}
我从这个答案中得出这个结论:https://stackoverflow.com/a/47777636/16241