从C#中的CURL获取JSON

本文关键字:获取 JSON CURL 中的 | 更新日期: 2023-09-27 18:20:24

我正试图通过CURL:获取JSON字符串

curl https://{subdomain}.zendesk.com/api/v2/ticket_fields/{id}.json '
-v -u {email_address}:{password}

我不知道如何将其转换为C#命令,但我一直在尝试使用WebClient和WebReJust类,但没有任何运气。

首先,我尝试通过WebClient:访问URL

var tags = api.Tickets.GetTicketFieldById(123456789);
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("email@domain.com", "abc123", "https://domain.zendesk.com/api/v2");
string json = client.DownloadString(tags.TicketField.Url);

但它抛出了401个未经授权的例外。

我对WebRequest/WebResponse尝试了同样的方法,但出现了同样的错误。我看到Zendesk提供了一个CURL字符串来访问数据,但我不确定如何将CURL字符串转换为C#命令。

有人能帮我访问返回的JSON字符串吗?

提前谢谢,杰斯珀。

从C#中的CURL获取JSON

string json;
using(var client = new WebClient())
{
    client.Credentials = new NetworkCredential("{email_address}", "{password}");
    json = client.DownloadString(
        "https://{subdomain}.zendesk.com/api/v2/ticket_fields/{id}.json");
}
// use json...

显然,您需要替换这些值。我想这不是问题所在。