2015-05-12 47 views
1

我试过用“ping”方法调用Mandrill的API来实现this的最简单情况。答案应该是乒乓球。使用有效的密钥,答复似乎工作。但是,我无法访问响应的内容。如何访问API响应的内容?

的代码如下:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net.Http; 
using System.Net.Http.Headers; 

namespace MandrillAPI 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string ping = "https://mandrillapp.com/api/1.0/users/ping.json"; 
      string key = "?key=123"; 
      HttpClient client = new HttpClient(); 
      client.BaseAddress = new Uri(ping); 
      HttpResponseMessage response = client.GetAsync(key).Result; 
      Console.WriteLine(response.ToString()); 
      Console.Read(); 
     } 
    } 
} 

如何解压从调用的响应?最终,我需要使用API​​从服务器收集电子邮件,所以我需要以某种方式保留json对象的结构,以便可以访问电子邮件的详细信息。

+1

你能否详细说明你期望的结果是什么? –

回答

1

如果你只是想读取响应作为一个字符串:

string content = response.Content.ReadAsStringAsync().Result 

如果你想反序列化一些类(在这种情况下,类型MyClass):

MyClass myClass = JsonConvert.DeserializeObject<MyClass>(response.Content.ReadAsStringAsync().Result) 
0

我会使用类似JSON.Net的东西来将它序列化为一个C#对象,然后可以使用它。