【问题标题】:HTTP header is being sent, but isn't present in Request.Headers正在发送 HTTP 标头,但 Request.Headers 中不存在
【发布时间】:2019-03-24 02:24:52
【问题描述】:

我的 api 端代码如下所示:

[HttpPost]
[Route("api/Login")]
public HttpResponseMessage ValidateLogin(UserModel user)
{
    IEnumerable<string> customJsonInputString;

    if (!Request.Headers.TryGetValues("Content-Type", out customJsonInputString))
        return new HttpResponseMessage(HttpStatusCode.BadRequest);

    var customJsonInputArray = customJsonInputString.ToArray();

    var ProductsRequest =
      Newtonsoft.Json.JsonConvert.DeserializeObject<UserModel>(customJsonInputArray[0]);

    var result = _service.Fetch(
            new UserModel
            {
                Username = user.Username,
                Password = user.Password.GenerateHash()
            }
        );
    return Request.CreateResponse(HttpStatusCode.OK, result);
}

我正在尝试从同一解决方案中的单独项目中调用它:

[HttpPost]
public async Task<ActionResult> Login(UserLoginModel user)
{
    UserModel data = new UserModel
    {
        Username = user.Username,
        Password = user.Password
    };

    using (var client = new HttpClient())
    {
        var myContent = JsonConvert.SerializeObject(data);
        var buffer = Encoding.UTF8.GetBytes(myContent);
        var byteContent = new ByteArrayContent(buffer);
        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        var endpoint = "http://localhost:55042/api/Login";

        var response = await client.PostAsync(endpoint, byteContent);

        throw new NotImplementedException();
    }
}

我认为问题出在Request.Headers.TryGetValues("Content-Type", out customJsonInputString)-s 第一个参数名称中,我已经在网上搜索过,但没有找到正确的描述/解释该参数名称应该是什么(嗯,我知道这是一个标头名称,但我也尝试使用“ContentType”找到它,结果相同:“400 bad request”),所以我的问题是:

  • 我做错了什么?
  • 我认为标题的名称是“ContentType”或“Content-Type”有错吗?

【问题讨论】:

标签: c# asp.net asp.net-mvc asp.net-web-api


【解决方案1】:

尝试像这样更新您的代码:

using (var client = new HttpClient())
        {
            var myContent = JsonConvert.SerializeObject(data);                
            var endpoint = "http://localhost:55042/api/Login";    
            var response = await client.PostAsync(endpoint, new StringContent(myContent, Encoding.UTF8,"application/json"));
        }

【讨论】:

    【解决方案2】:

    Content-Type 标头位于 Request.Content.Headers 中。您可以使用当前代码获取标头值或检查 Request.Content.Headers.ContentType 属性是否等于 null

    //note added Content
    if (!Request.Content.Headers.TryGetValues("Content-Type", out customJsonInputString))
        return new HttpResponseMessage(HttpStatusCode.BadRequest);
    

    if (Request.Content.Headers.ContentType == null)
        return new HttpResponseMessage(HttpStatusCode.BadRequest);
    

    ContentType 属性等于 null,即使设置了 Content-Type 标头但无效。

    【讨论】:

      【解决方案3】:

      我使用HttpWebRequest 而不是HttpClient,遇到了更多问题,但最终一切都解决了。 How i chose to proceed and next problem which is already solved

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2016-04-19
        • 2017-11-27
        • 2021-07-05
        • 2011-09-21
        • 2014-09-20
        相关资源
        最近更新 更多