如何将自定义标头添加到HttpClient()?

时间:2019-04-23 12:41:26

标签: c# asp.net-mvc api header custom-headers

我正在尝试向Web应用程序的请求标头添加自定义标头。在我的Web应用程序中,我从Web api检索数据,在此请求中,我想添加一个包含字符串sessionID的自定义标头。我正在寻找一种通用的解决方案,这样我就不必在每次拨打电话之前都添加相同的代码。

我的控制器如下:

[HttpGet]
    public async Task<ActionResult> getCall()
    {
        string url = "http://localhost:51080/";
        string customerApi = "customer/1";

        using (var client = new HttpClient())
        {
            //get logged in userID
            HttpContext context = System.Web.HttpContext.Current;
            string sessionID = context.Session["userID"].ToString();

            //Create request and add headers
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Custom header


            //Response
            HttpResponseMessage response = await client.GetAsync(customerApi);
            if (response.IsSuccessStatusCode)
            {
                string jsondata = await response.Content.ReadAsStringAsync();
                return Content(jsondata, "application/json");
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }
    }

希望任何人都能提供帮助!

谢谢!

2 个答案:

答案 0 :(得分:0)

DefaultRequestHeaders后面的集合具有Add方法,该方法可让您添加所需的标题:

client.DefaultRequestHeaders.Add("headerName", sesssionID);

答案 1 :(得分:0)

尝试一下:

[((1, 2), (2, 3), (3, 1)), ((1, 2), (2, 3), (3, 4)), ((1, 2), (2, 4), (3, 1)), ((1, 2), (2, 4), (3, 4)), ((1, 2), (2, 5), (3, 1)), ((1, 2), (2, 5), (3, 4)), ((1, 2), (2, 6), (3, 1)), ((1, 2), (2, 6), (3, 4)), ((1, 2), (3, 1), (2, 3)), ((1, 2), (3, 1), (2, 4)), ((1, 2), (3, 1), (2, 5)), ((1, 2), (3, 1), (2, 6)), ((1, 2), (3, 1), (3, 2)), ((1, 2), (3, 1), (3, 4)), ((1, 2), (3, 2), (3, 1)), ((1, 2), (3, 2), (3, 4)), ((1, 2), (3, 4), (2, 3)), ((1, 2), (3, 4), (2, 4)), ((1, 2), (3, 4), (2, 5)), ((1, 2), (3, 4), (2, 6)), ((1, 2), (3, 4), (3, 1)), ((1, 2), (3, 4), (3, 2)), ((2, 3), (1, 2), (3, 1)), ((2, 3), (1, 2), (3, 4)), ((2, 3), (2, 4), (3, 1)), ((2, 3), (2, 4), (3, 4)), ((2, 3), (2, 5), (3, 1)), ((2, 3), (2, 5), (3, 4)), ((2, 3), (2, 6), (3, 1)), ((2, 3), (2, 6), (3, 4)), ((2, 3), (3, 1), (1, 2)), ((2, 3), (3, 1), (2, 4)), ((2, 3), (3, 1), (2, 5)), ((2, 3), (3, 1), (2, 6)), ((2, 3), (3, 4), (1, 2)), ((2, 3), (3, 4), (2, 4)), ((2, 3), (3, 4), (2, 5)), ((2, 3), (3, 4), (2, 6)), ((2, 4), (1, 2), (3, 1)), ((2, 4), (1, 2), (3, 4)), ((2, 4), (2, 3), (3, 1)), ((2, 4), (2, 3), (3, 4)), ((2, 4), (2, 5), (3, 1)), ((2, 4), (2, 5), (3, 4)), ((2, 4), (2, 6), (3, 1)), ((2, 4), (2, 6), (3, 4)), ((2, 4), (3, 1), (1, 2)), ((2, 4), (3, 1), (2, 3)), ((2, 4), (3, 1), (2, 5)), ((2, 4), (3, 1), (2, 6)), ((2, 4), (3, 1), (3, 2)), ((2, 4), (3, 1), (3, 4)), ((2, 4), (3, 2), (3, 1)), ((2, 4), (3, 2), (3, 4)), ((2, 4), (3, 4), (1, 2)), ((2, 4), (3, 4), (2, 3)), ((2, 4), (3, 4), (2, 5)), ((2, 4), (3, 4), (2, 6)), ((2, 4), (3, 4), (3, 1)), ((2, 4), (3, 4), (3, 2)), ((2, 5), (1, 2), (3, 1)), ((2, 5), (1, 2), (3, 4)), ((2, 5), (2, 3), (3, 1)), ((2, 5), (2, 3), (3, 4)), ((2, 5), (2, 4), (3, 1)), ((2, 5), (2, 4), (3, 4)), ((2, 5), (2, 6), (3, 1)), ((2, 5), (2, 6), (3, 4)), ((2, 5), (3, 1), (1, 2)), ((2, 5), (3, 1), (2, 3)), ((2, 5), (3, 1), (2, 4)), ((2, 5), (3, 1), (2, 6)), ((2, 5), (3, 1), (3, 2)), ((2, 5), (3, 1), (3, 4)), ((2, 5), (3, 2), (3, 1)), ((2, 5), (3, 2), (3, 4)), ((2, 5), (3, 4), (1, 2)), ((2, 5), (3, 4), (2, 3)), ((2, 5), (3, 4), (2, 4)), ((2, 5), (3, 4), (2, 6)), ((2, 5), (3, 4), (3, 1)), ((2, 5), (3, 4), (3, 2)), ((2, 6), (1, 2), (3, 1)), ((2, 6), (1, 2), (3, 4)), ((2, 6), (2, 3), (3, 1)), ((2, 6), (2, 3), (3, 4)), ((2, 6), (2, 4), (3, 1)), ((2, 6), (2, 4), (3, 4)), ((2, 6), (2, 5), (3, 1)), ((2, 6), (2, 5), (3, 4)), ((2, 6), (3, 1), (1, 2)), ((2, 6), (3, 1), (2, 3)), ((2, 6), (3, 1), (2, 4)), ((2, 6), (3, 1), (2, 5)), ((2, 6), (3, 1), (3, 2)), ((2, 6), (3, 1), (3, 4)), ((2, 6), (3, 2), (3, 1)), ((2, 6), (3, 2), (3, 4)), ((2, 6), (3, 4), (1, 2)), ((2, 6), (3, 4), (2, 3)), ((2, 6), (3, 4), (2, 4)), ((2, 6), (3, 4), (2, 5)), ((2, 6), (3, 4), (3, 1)), ((2, 6), (3, 4), (3, 2)), ((3, 1), (1, 2), (2, 3)), ((3, 1), (1, 2), (2, 4)), ((3, 1), (1, 2), (2, 5)), ((3, 1), (1, 2), (2, 6)), ((3, 1), (1, 2), (3, 2)), ((3, 1), (1, 2), (3, 4)), ((3, 1), (2, 3), (1, 2)), ((3, 1), (2, 3), (2, 4)), ((3, 1), (2, 3), (2, 5)), ((3, 1), (2, 3), (2, 6)), ((3, 1), (2, 4), (1, 2)), ((3, 1), (2, 4), (2, 3)), ((3, 1), (2, 4), (2, 5)), ((3, 1), (2, 4), (2, 6)), ((3, 1), (2, 4), (3, 2)), ((3, 1), (2, 4), (3, 4)), ((3, 1), (2, 5), (1, 2)), ((3, 1), (2, 5), (2, 3)), ((3, 1), (2, 5), (2, 4)), ((3, 1), (2, 5), (2, 6)), ((3, 1), (2, 5), (3, 2)), ((3, 1), (2, 5), (3, 4)), ((3, 1), (2, 6), (1, 2)), ((3, 1), (2, 6), (2, 3)), ((3, 1), (2, 6), (2, 4)), ((3, 1), (2, 6), (2, 5)), ((3, 1), (2, 6), (3, 2)), ((3, 1), (2, 6), (3, 4)), ((3, 1), (3, 2), (1, 2)), ((3, 1), (3, 2), (2, 4)), ((3, 1), (3, 2), (2, 5)), ((3, 1), (3, 2), (2, 6)), ((3, 1), (3, 4), (1, 2)), ((3, 1), (3, 4), (2, 4)), ((3, 1), (3, 4), (2, 5)), ((3, 1), (3, 4), (2, 6)), ((3, 2), (1, 2), (3, 1)), ((3, 2), (1, 2), (3, 4)), ((3, 2), (2, 4), (3, 1)), ((3, 2), (2, 4), (3, 4)), ((3, 2), (2, 5), (3, 1)), ((3, 2), (2, 5), (3, 4)), ((3, 2), (2, 6), (3, 1)), ((3, 2), (2, 6), (3, 4)), ((3, 2), (3, 1), (1, 2)), ((3, 2), (3, 1), (2, 4)), ((3, 2), (3, 1), (2, 5)), ((3, 2), (3, 1), (2, 6)), ((3, 2), (3, 4), (1, 2)), ((3, 2), (3, 4), (2, 4)), ((3, 2), (3, 4), (2, 5)), ((3, 2), (3, 4), (2, 6)), ((3, 4), (1, 2), (2, 3)), ((3, 4), (1, 2), (2, 4)), ((3, 4), (1, 2), (2, 5)), ((3, 4), (1, 2), (2, 6)), ((3, 4), (1, 2), (3, 1)), ((3, 4), (1, 2), (3, 2)), ((3, 4), (2, 3), (1, 2)), ((3, 4), (2, 3), (2, 4)), ((3, 4), (2, 3), (2, 5)), ((3, 4), (2, 3), (2, 6)), ((3, 4), (2, 4), (1, 2)), ((3, 4), (2, 4), (2, 3)), ((3, 4), (2, 4), (2, 5)), ((3, 4), (2, 4), (2, 6)), ((3, 4), (2, 4), (3, 1)), ((3, 4), (2, 4), (3, 2)), ((3, 4), (2, 5), (1, 2)), ((3, 4), (2, 5), (2, 3)), ((3, 4), (2, 5), (2, 4)), ((3, 4), (2, 5), (2, 6)), ((3, 4), (2, 5), (3, 1)), ((3, 4), (2, 5), (3, 2)), ((3, 4), (2, 6), (1, 2)), ((3, 4), (2, 6), (2, 3)), ((3, 4), (2, 6), (2, 4)), ((3, 4), (2, 6), (2, 5)), ((3, 4), (2, 6), (3, 1)), ((3, 4), (2, 6), (3, 2)), ((3, 4), (3, 1), (1, 2)), ((3, 4), (3, 1), (2, 4)), ((3, 4), (3, 1), (2, 5)), ((3, 4), (3, 1), (2, 6)), ((3, 4), (3, 2), (1, 2)), ((3, 4), (3, 2), (2, 4)), ((3, 4), (3, 2), (2, 5)), ((3, 4), (3, 2), (2, 6))]