如何从.Net Core 3.1代码进行cURL命令调用 [英] How to I make a cURL command call from .Net Core 3.1 Code
问题描述
我有一个复杂的cURL命令,可以正常运行:
I have this complicated cURL command that is working perfectly:
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命令并获取响应.
However it is not really feasible to replicate in C# using HttpClient. But I need to call the cURL command from .Net Core code and get the response back.
是否可以在.Net Core(v3.1)中进行正常的cURL命令调用?
(In case you are interested, this is the details on the cURL command: Replicate cURL Command Using Redirect and Cookies in .Net Core 3.1)
推荐答案
这可以通过对powershell的集成调用来完成.
This can be done via an integrated call to 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
I drew this from this answer: https://stackoverflow.com/a/47777636/16241
这篇关于如何从.Net Core 3.1代码进行cURL命令调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!