2015-06-05 78 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