2015-06-05 32 views
0

我一直在关注这个Making a cURL call in C#试图让与卷曲的ID登录API的请求和接收我的C#应用​​程序的结果,C#请求 - API ID登录

请求应被填上以下内容作为解释在:http://developers.livechatinc.com/rest-api/#!introduction

curl "https://api.livechatinc.com/agents" \ 
    -u [email protected]: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()); 
    } 
} 

结果似乎是百达相同的 “无法开机自检/代理”

+0

我想你想要做一个GET请求。不是POST。 POST用于创建新的代理程序:http://developers.livechatinc.com/rest-api/#create-agent(并且需要您发送定义要创建的新代理程序的JSON请求负载)。你想这个:http://developers.livechatinc.com/rest-api/#get-single-agent –

+0

@OliverMcPhee你是对的,但我怎么能添加“-u [email protected]:c14b85863755158d7aa5cc4ba17f61cb \ -H X-API-Version:2“ using a client.GetAsync method – EchO

+0

看看这个线程的答案:http://stackoverflow.com/questions/12022965/adding-http-headers-to-httpclient -Asp网的Web-API –

回答

1

您正在执行POST操作。这是保留创建一个新的代理,并要求您发送一个JSON请求有效载荷。在这里看到: developers.livechatinc.com/rest-api/#create-agent

你想要做什么是一个GET操作: developers.livechatinc.com/rest-api/#get-single-agent

而不是使用PostAsync的,你需要创建一个HttpRequestMessage,设置方法获取,设置你的头,然后用SendAsync。在这里看到的解决方案:Adding Http Headers to HttpClient

记住,对于REST API:

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