C# 请求 - API LiveChat

C# Request - API LiveChat

我一直在关注此 Making a cURL call in C# 以尝试使用 curl 中的 LiveChat API 发出请求并在我的 C# 应用程序上接收结果,

请求应按以下说明填写:http://developers.livechatinc.com/rest-api/#!introduction

curl "https://api.livechatinc.com/agents" \
    -u john.doe@mycompany.com:c14b85863755158d7aa5cc4ba17f61cb \
    -H X-API-Version:2 

这是我在 C# 中所做的:

static void Main(string[] args)
{
    RequestTest();
    Console.ReadKey();
}

private static async void RequestTest()
{
    var client = new HttpClient();

    // Create the HttpContent for the form to be posted.
    var requestContent = new FormUrlEncodedContent(new[] {new KeyValuePair<string, string>("myemail:myapikey", "X-API-Version:2"),});

    // Get the response.
    HttpResponseMessage response = await client.PostAsync(
        "https://api.livechatinc.com/agents",
        requestContent);

    // Get the response content.
    HttpContent responseContent = response.Content;

    // Get the stream of the content.
    using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
    {
        // Write the output.
        Console.WriteLine(await reader.ReadToEndAsync());
    }
}

结果似乎总是一样"Cannot POST to /agents"

您正在此处执行 POST 操作。这是为创建新代理而保留的,需要您发送 JSON 请求负载。看这里: developers.livechatinc.com/rest-api/#create-agent

你要做的是GET操作: developers.livechatinc.com/rest-api/#get-single-agent

您需要创建一个 HttpRequestMessage,而不是使用 PostAsync,将方法设置为 GET,设置您的 headers,然后使用 SendAsync。在此处查看解决方案:Adding Http Headers to HttpClient

请记住,对于 REST API:

POST = Create Operations,
GET = Read Operations,
PUT = Update Operations,
DELETE = Delete Operations