HttpRequestMessage多个自定义标头互相覆盖

时间:2015-07-09 02:03:07

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

我很难理解如何将MULTIPLE标头添加到HttpRequestMessage中。我的意思是我得到了基础知识,如果我做了一个标题它可以工作但是多个标题,最后一个是什么棒,所有其他标题都被覆盖了?

因此,例如,Accept标头将被覆盖,但授权标头将会粘住。

theDict = {'cereal': ['as', '1w', '45'],
           'pop': ['gh', 'er', '65'],
           'crackle': ['yu', 'po', '22']}

for key, value in theDict.items():
    if 'po' in value:
        print(key)

现在HttpRequestMessage有一个带签名的重载

HttpRequestMessage httpreqmsg = new HttpRequestMessage();
httpreqmsg.Headers.Add("Accept", "CustomAccept");
httpreqmsg.Headers.Add("Authorization", "asdfas#%fwqerq@werfds...");

如果您有一个具有多个值的名称,那么这很好,但是您如何做多个标题。 TryAddWithoutValidation具有与上面相同的重载?

TIA

太棒了......所以我在帖子中犯了一个错误。我认为这不重要,但确实如此。我是单元测试控制器,因此没有创建HttpClient对象。

3 个答案:

答案 0 :(得分:3)

似乎"接受"标头是保留的。无论我试图为它分配什么值,都抛出了FormatException。

如果您将代码更改为此代码,则会返回两个标题。

        HttpRequestMessage httpreqmsg = new HttpRequestMessage();
        httpreqmsg.Headers.Add("Lolz", "CustomAccept");
        httpreqmsg.Headers.Add("Authorization", "SomeValue");

        foreach (var item in httpreqmsg.Headers)
        {
            Console.WriteLine(item.Key + " : " + item.Value);
        }

还有“授权”的价值。在你的例子中是无效的,但我猜这只是随机密钥捣碎:)

答案 1 :(得分:3)

不确定您的代码的上下文,但您可以使用HttpClient(WebApi NuGet),这可以让您添加"接受"和#34;授权"头:

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("scheme", "param");

                //client.PostAsJsonAsync or something else

            }

答案 2 :(得分:2)

以@ sidjames的帖子为基础。您似乎正在寻找Accept和Authorization标头。如果是这种情况,那么在HttpClient中设置它,而不是HttpRequestMessage:

    <nav class="navbar navbar-inverse" role="banner">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span> <span
                        class="icon-bar"></span> <span class="icon-bar"></span> <span
                        class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="index.jsp"><img src="../img/waytowealth_green1.png" alt="logo" /> </a>
            </div>

            <div class="collapse navbar-collapse navbar-right">
                <ul id="ulOptions" class="nav navbar-nav pull-right">
                    <li <c:out value="${param.page=='Home'?'class=active':''}"/> ><a href="index.jsp?page=Home">Home</a></li>
                    <li <c:out value="${param.page=='MutualFunds'?'class=active':''}"/>><a href="MutualFunds.jsp?page=MutualFunds">Mutual Funds</a></li>
                    <li <c:out value="${param.page=='FP'?'class=active':''}"/>><a href="FinancialPlanning.jsp?page=FP">Financial Planning</a></li>
                    <li <c:out value="${param.page=='Insurance'?'class=active':''}"/>><a href="Insurance.jsp?page=Insurance">Insurance</a></li>
                    <li class="dropdown <c:out value="${param.page=='calculator'?'active':''}"/>"><a href="#" class="dropdown-toggle"
                        data-toggle="dropdown">Calculators<i class="fa fa-angle-down"></i></a>
                        <ul class="dropdown-menu">
                            <li><a href="m_sip.jsp?page=calculator">Monthly Systematic Investment
                                    Plan</a></li>
                            <li><a href="lumpsum_fv.jsp?page=calculator">One Time Investment
                                    Calculator</a></li>
                            <li><a href="m_sip_fv.jsp?page=calculator">Final Value of Monthly SIP
                                    Calculator</a></li>
                        </ul></li>
                    <li <c:out value="${param.page=='Downloads'?'class=active':''}"/>><a href="Downloads.jsp?page=Downloads">Downloads</a></li>
                    <li><a href="Downloads.jsp">Articles</a></li>
                    <li <c:out value="${param.page=='ContactUs'?'class=active':''}"/>><a href="ContactUs.jsp?page=ContactUs">Contact Us</a></li>
                    <li <c:out value="${param.page=='AboutUs'?'class=active':''}"/>><a href="AboutUs.jsp?page=AboutUs">About Us</a></li>
                </ul>
            </div>
        </div>
        <!--/.container-->
    </nav>

StackOverflow上有acceptauthorization的大量示例。