【问题标题】:Add x-api-key to request HTTP header using web.config使用 web.config 添加 x-api-key 以请求 HTTP 标头
【发布时间】:2020-03-03 15:25:05
【问题描述】:

我尝试使用的 Web 服务要求我将 x-api-key 添加到请求的 HTTP 标头中。 是否可以使用 web.config 将此新标头添加到请求中?我尝试将 header 元素添加到端点,如下例所示,但不断收到 403-Forbidden:

<endpoint address="webserviceurl"
    behaviorConfiguration="myBehavior" binding="customBinding"
    bindingConfiguration="myBinding" contract="myContract"
    name="serviceName">
    <headers>
      <x-api-key xmlns="webserviceurl">"key"</x-api-key>
    </headers>
</endpoint>

【问题讨论】:

  • @DanielA.White 关于 web.config 的两个答案正是我正在做的。
  • 也许从值中去掉引号?
  • 同样的事情。我认为问题在于它添加到 SOAP 标头,而不是 HTTP 标头。

标签: c# wcf


【解决方案1】:

通过web.config找不到办法,所以我根据this answer开发了一个解决方案:

WSClient client = new WSClient("endpointName");

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
    WebOperationContext.Current.OutgoingRequest.Headers.Add("X-API-KEY", "key");
    WSRequest req = new WSRequest();
    WSResponse resp = client.WSMethod(req);
}  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 2014-12-20
    • 1970-01-01
    • 2012-12-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    相关资源
    最近更新 更多