【问题标题】:How to add new key value pair in request headers for Web API?如何在 Web API 的请求标头中添加新的键值对?
【发布时间】:2014-08-25 14:01:14
【问题描述】:

我的api调用代码是这样的

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:56851/");

//Here I want add Authentication Header as new key value pair 
//Something similar like this 
//client.Headers["AuthenticationToken"]="FASFASFA#$";

        var id = txtSearch.Text.Trim();
        var url = "api/User/" + id;
        HttpResponseMessage response = client.GetAsync(url).Result;
        if (response.IsSuccessStatusCode)
        {
            var users = response.Content.ReadAsAsync<Users>().Result;

            MessageBox.Show("User Found : " + 
            users.FirstName + " "  + users.LastName);
        }

如何在 Web API 的请求头中添加新的键值对?

【问题讨论】:

    标签: c# asp.net-web-api2


    【解决方案1】:

    您可以使用HttpClientDefaultRequestHeaders.Authorization 属性:

    client.DefaultRequestHeaders.Authorization = 
              new AuthenticationHeaderValue("YourAuthenticationScheme", "FASFASFA#$");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-02
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 2015-08-30
      • 2013-12-31
      • 2019-07-16
      相关资源
      最近更新 更多