![](/img/trans.png)
[英]Calling Web Service from a C# Console application. The website is secure with form Authentication
[英]Calling a web service from the C# console application
我正在尝试调用 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;
在这里,我需要从服务调用中获取响应并将它们存储到字符串中。 就像每个字段响应字符串一样。 我该如何进一步。
您需要阅读您收到的回复内容:
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsAsync<RootObject>().Result;
您还应该在Get
方法上添加 async 关键字以使用 async/await 模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.