我正在尝试调用Web服务并将json响应添加到我的应用程序中。我为JSON响应的get set属性创建了类。以下是我正在尝试的内容
public class iResponse
{
public iResponse(){ }
public List<iDTO> data { get; set; }
}
public class iDTO
{
public iDTO() { }
public class Field
{
public string label { get; set; }
public string type { get; set; }
public string name { get; set; }
}
public class CForm
{
public int id { get; set; }
public string name { get; set; }
public string note { get; set; }
public List<Field> fields { get; set; }
}
public class IlMetadata
{
}
public class IlResponse
{
public List<CustomForm> custom_forms { get; set; }
public IlMetadata il_metadata { get; set; }
}
public class RootObject
{
public IlResponse il_response { get; set; }
}
尝试调用下面的服务
public class iAPICall
{
string BaseURL= ConfigurationManager.AppSettings["BaseURL"];
string accessToken = ConfigurationManager.AppSettings["AccessToken"];
public void Get()
{
using (var iab = new HttpClient())
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
Uri uri = new Uri(BaseURL);
iab.BaseAddress = uri;
iab.DefaultRequestHeaders.Accept.Clear();
iab.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
iab.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
var response = iab.GetAsync(BaseURL).Result;
这里我需要从服务调用中获取响应并将它们存储到字符串中。像响应字符串中的每个字段一样。我该怎么办呢。
答案 0 :(得分:0)
您需要阅读所获得的回复内容:
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsAsync<RootObject>().Result;
您还应该在Get
方法上添加async关键字,以便使用async / await模式。