如何使用 C# 生成 curl 命令

How to generate curl command using C#

我正在使用 Salesforce 并希望在其中获取个案字段。找到命令

curl https://yoursite.desk.com/api/v2/cases \
-u email:password \
-H 'Accept: application/json'

我在命令提示符下尝试了

curl https://xxx.desk.com/api/v2/cases \-u abc@gmail.com:xxxxxxxxx \ -H 'Accept: application/json'

我已经在 C# 中尝试了以下代码

        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://xxxx.desk.com/api/v2/cases");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Accept = "text/xml";
        httpWebRequest.Method = "GET";
        httpWebRequest.Credentials = new NetworkCredential("username", "password");
        var response = httpWebRequest.GetResponse();

但是返回错误

The remote server returned an error: (401) Unauthorized.

CURL 只是 WebRequests 的客户端

默认的 C# 选项是 WebRequest Class 你也可以使用 Restsharp

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://xxxx.desk.com/api/v2/cases");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Accept = "text/xml";
        httpWebRequest.Method = "GET";
        httpWebRequest.Credentials = new NetworkCredential("username", "password");
        httpWebRequest.Headers.Add("Authorization", "Basic reallylongstring");
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        string text;
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {            
                string fddf =  streamReader.ReadToEnd();

        }