设置HttpWebRequest头

问题描述 投票:0回答:1

我想发送一个POST请求到一个指定的Endpoint。为了授权,我必须在请求头设置一个 "x-api-key: key-value pair"。

这就是我正在使用的方法。

public string postXMLData(string destinationUrl, string requestXml)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);

            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
            request.ContentType = "text/xml; encoding='utf-8'";
            request.ContentLength = bytes.Length;
            request.Method = "POST";

...
c# httpwebrequest webrequest
1个回答
0
投票

你只需要添加

request.Headers.Add("x-api-key", "the secret key");

其中 "the secret key" 是您的API密钥。

© www.soinside.com 2019 - 2024. All rights reserved.