【问题标题】:How to generate curl command using C#如何使用 C# 生成 curl 命令
【发布时间】:2015-08-06 13:42:06
【问题描述】:

我正在使用 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.

【问题讨论】:

    标签: c# json salesforce


    【解决方案1】:

    CURL 只是 WebRequests 的客户端

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

    【讨论】:

    • 我不能为你做你的工作。这些是工具。有例子和教程。如果你遇到一个新问题,请问。
    【解决方案2】:
            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();
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2014-10-18
      • 2017-02-27
      相关资源
      最近更新 更多