如何从.Net Core 3.1代码进行cURL命令调用 [英] How to I make a cURL command call from .Net Core 3.1 Code

查看:53
本文介绍了如何从.Net Core 3.1代码进行cURL命令调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的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命令调用?

(如果您感兴趣,这是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.

  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);
}

我是从以下答案中得出的: https://stackoverflow.com/a/47777636/16241

I drew this from this answer: https://stackoverflow.com/a/47777636/16241

这篇关于如何从.Net Core 3.1代码进行cURL命令调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆