如何在.net 中跨平台调用webapi接口

背景:在开发项目的过程当中,会有项目与项目之间的对接与调用

列:对接项目之前基础数据的接口 

1.创建相同的实体去接收传过来的数据

 

实体:

public class SyncEmp
{
public string EmployeeId { get; set; }

public string EmployeeNum { get; set; }

public string EmployeeName { get; set; }

public string CompanyId { get; set; }

public string Gender { get; set; }

public string OrganizationId { get; set; }

public string EmployeeStatus { get; set; }

public string Mobile { get; set; }

public string Mail { get; set; }

public string Position { get; set; }

public string PhotoId { get; set; }

public DateTime? AttendDate { get; set; }

public DateTime? ProbationEndDate { get; set; }

public DateTime? DimissionDate { get; set; }

public string IsLock { get; set; }

public string CreateTime { get; set; }

public string CreateUser { get; set; }

public string UpdateTime { get; set; }

public string UpdateUser { get; set; }

public string JobLevel { get; set; }

public string JobFamily { get; set; }

public List<ReportingLine> reportLine { get; set; }

}

public class ReportingLine
{
public string EmployeeId { get; set; }

public string ReportingLineEmployeeId { get; set; }

public string ReportingLineCode { get; set; }
}

 

var httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(0, 1, 0);//设置读取连接超时时间

  string url = ConfigurationManager.AppSettings["hrcore"];//域名

 

    var response = httpClient.GetAsync(url + $@"api/v1/person/{code}/{timeStamp}/n{number}/{endTime}").Result;//api接口

   var result = response.Content.ReadAsStringAsync().Result;//返回结果

    list = JsonConvert.DeserializeObject<List<SyncEmp>>(result.ToString());//转换成实体(如果实体中的字段跟返回结果中的字段不一样,那么就接收不到值)

例2(接收泛型的实体):

public class TableData
{
public TablePage page { get; set; }
public IEnumerable<dynamic> data { get; set; }
}

方法

public TableData GetUserLists(int pageNum, int pageSize, string sort, string search)
{
var httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(0, 1, 0);
TableData data = null;
var userData = userAndRoleRelationDal.GetUserAndRoleRelationData();
string url = System.Configuration.ConfigurationManager.AppSettings["UserUrl"];
search = HttpUtility.UrlEncode(search, Encoding.GetEncoding("UTF-8"));
var response = httpClient.GetAsync(url + $@"api/usercenter/user/list/{CompanyCode.TrimEnd('_').ToString()}?pageNum={pageNum}&pageSize={pageSize}&sort={sort}&search={search}").Result; //返回结果({"result":true,"errorCode":"GWUC_00000","errorMsg":"Operation succeeded","data":{"page":{"pageNum":1,"total":6},"data":[{"tenant":"lynn","userName":"10001","personID":"0bb8b98b-019b-4d0e-8377-5f1cdda94685","effectiveDate":"2018-01-01","expiryDate":"2019-03-31","employeeName":"李梦","isEnabled":1,"simCardNum":"","mobileID":"","registrationID":"","accID":"","areaCode":"","mobile":"","email":null,"isModifyPassword":0,"passwordExpiryDate":"","isTwoFactor":0,"userID":"8cee9bea-10c0-407a-bca7-e80b1618d0d7","userType":"","roleId":"","roles":[],"employeeID":null,"displayName":null,"unitName":null,"jobName":null,"photo":null,"createTimestamp":1542194049087},{"tenant":"lynn","userName":"10019","personID":"11c60a0c-9ab3-4aa1-8dfc-d2b07b3e7a4f","effectiveDate":"2018-01-01","expiryDate":"2019-03-31","employeeName":"刘建国","isEnabled":1,"simCardNum":"","mobileID":"","registrationID":"","accID":"","areaCode":"","mobile":"","email":null,"isModifyPassword":0,"passwordExpiryDate":"","isTwoFactor":0,"userID":"2c59df75-e4c0-4d3c-a765-77e9535d2b70","userType":"","roleId":"","roles":[],"employeeID":null,"displayName":null,"unitName":null,"jobName":null,"photo":null,"createTimestamp":1545808800435}]}})

 

var result = response.Content.ReadAsStringAsync().Result;
JObject jobj = JObject.Parse(result);
if (jobj["result"].ToString() == "false")//判断结果是否为空
{
data = null;
}
else
{
data = JsonConvert.DeserializeObject<TableData>(jobj["data"].ToString());
}
if (data != null && data.data != null)
{
foreach (var item in data.data)
{
var roles = userData.Where(f => f.UserId == Convert.ToString(item.userID)).FirstOrDefault();
item.roleId = roles?.RoleId == null ? null : roles?.RoleId;
}
}
return data;
}

posted @ 2019-01-04 11:15  夜阑听风雨  阅读(660)  评论(0编辑  收藏  举报