简体   繁体   English

(UWP)如何在GET请求中添加不带Content-Length的Content-Type标头

[英](UWP) How to add Content-Type header without Content-Length in GET request

I'm dealing with a third-party API who's calls require the Content-Type header on GET method requests, but return an error response when the header collection includes "Content-Length". 我正在处理一个第三方API,该API的调用要求GET方法请求上的Content-Type标头,但是当标头集合包含“ Content-Length”时返回错误响应。 The only way to add the Content-Type header is to add a class that inherits/implements the IHttpContent interface (I was using HttpStringContent with string.empty as the string content). 添加Content-Type标头的唯一方法是添加一个继承/实现IHttpContent接口的类(我使用HttpStringContent并将string.empty作为字符串内容)。 The problem, is that adding a blank HttpStringContent adds ContentLength. 问题在于,添加空白HttpStringContent会添加ContentLength。 Even though the value of that header is '0', their server gets super angry. 即使该标头的值为“ 0”,他们的服务器也会非常生气。 Content-Length doesn't show up in the header collection while i'm debugging, but when i run my application through Postman or Burp Proxy, the Content-Length header is there. 在调试时,Content-Length不会显示在标头集合中,但是当我通过Postman或Burp Proxy运行我的应用程序时,Content-Length标头就在那里。 I've tried blindly using .Remove to get rid of the Content-Length header but that doesn't work. 我尝试盲目地使用.Remove摆脱Content-Length标头,但这不起作用。

I've seen StackOverflow questions about adding Content-Length, but that's not what i need/want. 我已经看到StackOverflow有关添加Content-Length的问题,但这不是我需要/想要的。 Is there any way to have the Content-Type header in a GET request, without having the Content-Length header? 有什么方法可以在GET请求中包含Content-Type标头, 而不必具有Content-Length标头? Or.... is there a way to remove the Content-Length header? 或者....有没有办法删除Content-Length标头? I've tried creating my own content class that implements IHttpContent, but i'm getting stuck with the implementation of IAsyncOperationWithProgress. 我尝试创建自己的实现IHttpContent的内容类,但是我对IAsyncOperationWithProgress的实现陷入困境。

I understand this isn't standard, which is why i'm having so much difficulty with this. 我知道这不是标准,这就是为什么我在此方面遇到很多困难。 I've also tried Flurl which also didn't add the Content-Type header. 我也尝试过Flurl,它也没有添加Content-Type标头。

TL;DR: Content-Type (application/json) is mandatory, and Content-Length results (even though it's 0) in API errors. TL; DR:Content-Type(application / json)是强制性的,Content-Length会导致API错误(即使它为0)。 How do i add Content-Type to Windows.Web.Http.HttpRequestMessage/HttpClient without Content-Length? 如何不使用Content-Length将Content-Type添加到Windows.Web.Http.HttpRequestMessage / HttpClient?

EDIT: for clarification this is a UWP application 编辑:为了澄清这是一个UWP应用程序

As you're aware, you're needing to do something non-standard, so I present a non-standard solution to get this done. 如您所知,您需要做一些非标准的事情,所以我提出了一个非标准的解决方案来完成这项工作。

You've likely tried client.DefaultRequestHeaders.Add() and client.DefaultRequestHeaders.TryAddWithoutValidation() without success. 您可能尝试了client.DefaultRequestHeaders.Add()client.DefaultRequestHeaders.TryAddWithoutValidation()失败。 You can use reflection to evil your way into HttpRequestHeaders to modify the validation behavior. 您可以使用反射来破坏进入HttpRequestHeaders的方式来修改验证行为。

static void AllowInvalidRequestHeader(string header)
{
    var headerType = typeof(HttpRequestHeaders);

    var field = headerType
        .GetField("invalidHeaders", System.Reflection.BindingFlags.NonPublic | 
                                    System.Reflection.BindingFlags.Static) ?? 
                headerType
        .GetField("s_invalidHeaders", System.Reflection.BindingFlags.NonPublic | 
                                      System.Reflection.BindingFlags.Static);

    if (field == null) return;

    var invalidFields = (HashSet<string>)field.GetValue(null);

    invalidFields.Remove(header);
}

In application initialization, call AllowInvalidRequestHeader("Content-Type") once, and initialize HttpClient instances with this: 在应用程序初始化中,调用AllowInvalidRequestHeader("Content-Type") ,并使用以下方法初始化HttpClient实例:

var client = new HttpClient();
client.DefaultRequestHeaders.Add("Content-Type", "application/json");

In a simple server side page that echos have request headers, I get back: 在回显具有请求标头的简单服务器端页面中,我得到了返回:

<div class="row">
    <p>Connection: Keep-Alive</p>
    <p>Content-Type: application/json</p>
    <p>Host: localhost:52975</p>
    <p>MS-ASPNETCORE-TOKEN: de3a3a58-eccc-407b-b5f7-9a6663631587</p>
    <p>X-Original-Proto: http</p>
    <p>X-Original-For: 127.0.0.1:53362</p>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Content-Length,Content-Type和Last-Modified添加到HTTP响应消息头中 - How to add the Content-Length,Content-Type and Last-Modified to the HTTP Response Message Header 如何将Content-Type标头添加到.Net GET Web请求? - How to add the Content-Type header to a .Net GET web request? 将内容类型标头添加到 httpclient GET 请求 - Add content-type header to httpclient GET request Flurl获取带有“ Content-Type”标头的请求? - Flurl get request with “Content-Type” header? restsharp PUT 请求错误--&gt;“StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)” - restsharp PUT request error --> "StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)" API响应:StatusCode:0,Content-Type :, Content-Length:0 - API response : StatusCode: 0, Content-Type: , Content-Length: 0 如何为 HttpClient 请求设置 Content-Type header? - How do you set the Content-Type header for an HttpClient request? 内容类型-WebAPI-请求标头 - Content-Type - WebAPI - Request Header 如何在 ASP.NET MVC 中使用 GET 请求发送 Content-Type 标头? - How can I send Content-Type header with a GET request in ASP.NET MVC? 在不使用 Content-Length 标头的情况下在下载前获取文件大小 - Get file size before download without using Content-Length header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM