HTTPCLIENT-将Content-Type添加到标头

时间:2018-08-27 16:35:04

标签: c# post httpclient content-type

我有一个要尝试连接的API,需要满足以下条件:

**** Although Content-Type does not have to be part of the Signature string, it is required as a HTTP Header if HTTP Method Type is POST. If the request is a standard POST (i.e. not a header ‘X-HTTPMethod-Override’POST, then the Content-Type should be set to ‘application/json’ or ‘application/json;charset=UTF-8’

它们给出了HTTP标头应为什么样的示例

-示例HTTP请求
开机自检/ xxx / xxx 主持人:www.xxx.ie 日期:2017年10月4日星期三16:35:51 BST 内容类型:application / json; charset = UTF-8

我已经尝试过了

     // (2) create the httprequest
        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
        signatureHeaders.Add("(request-target)");
        // (3) add the headers
        requestMessage.Headers.Add("host", hostheader);
        string date = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zzzz");
        requestMessage.Headers.Add("date", date);
        requestMessage.Headers.TryAddWithoutValidation("Content-Type", "application/json");
        HttpHeaders headers = requestMessage.Headers;
        IEnumerable<string> values;
        foreach (var header in requestMessage.Headers)
        {
            if (headers.TryGetValues(header.Key, out values))
            {
                string session = values.ToString();
            }
        }

但是永远不会添加“ Content-Type”标头。
我在做什么错??

谢谢

0 个答案:

没有答案