zoukankan      html  css  js  c++  java
  • httpclient 调用WebAPI

    1、创建webapi项目,提供接口方法如下:

    /// <summary>
    /// 获取租户、位置下的所有传感器
    /// </summary>
    /// <returns></returns>
    [AcceptVerbs("POST")]
    [Route("api/Sensors/GetSensors")]
    public JsonResponseBase<List<TenSensorDTO>> GetSensors()
    {
    Logger.Debug("SensorsController---------------GetSensors");
    string jsonData = this.Request.Content.ReadAsStringAsync().Result;
    Logger.Information(jsonData);
    TenSensorSearchDTO search = null;
    var response = new JsonResponseBase<List<TenSensorDTO>>();
    try
    {
    search = JsonConvert.DeserializeObject<TenSensorSearchDTO>(jsonData);
    response.Json.IsSuccess = true;
    }
    catch(Exception ex)
    {
    Logger.Fatal(ex);
    response.Json.IsSuccess = false;
    response.Json.OperationDesc = "参数错误";
    }
    if (response.Json.IsSuccess && search != null)
    {
    response = TenantManageService.GetSensorList(search);
    }

    return response;
    }

    2、客户端使用httpclient调用

    string url = urlPre + "Sensors/GetSensors";
    //租户ID必须传入,LocationID可以不传入,看调用段需求
    var content = new { TenantID = Guid.Parse("5ebf5f81-ac69-418c-b170-9eb255201dd1") ,
    LocationID = Guid.Parse("db5a58c0-1d12-4fe9-99bb-4bebe6bcb935") };
    var requestJson = JsonConvert.SerializeObject(content);
    HttpContent httpContent = new StringContent(requestJson);
    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    var httpClient = new HttpClient();
    var responseJson = httpClient.PostAsync(url, httpContent)
    .Result.Content.ReadAsStringAsync().Result;

  • 相关阅读:
    同一内网不能网段ping 不通
    mysql 5.6.33 重置密码后报错
    设置tomcat内存设定
    python 取两数的百分比
    cache buffers
    登录到mysql查看binlog日志
    mysqlbinlog 查看日志时发生报错
    find 查找文件 -exec 然后压缩 查看tar包的内容
    zip 压缩文件 unzip查看zip压缩包内的内容
    react-native 完整实现登录功能
  • 原文地址:https://www.cnblogs.com/wangchaozhi/p/5104587.html
Copyright © 2011-2022 走看看