首页 > 解决方案 > 设置 HttpWebRequest 标头

问题描述

我想向指定的端点发送 POST 请求。对于授权,我必须将“x-api-key:键值对”设置为我的请求标头。

这就是我正在使用的:

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#httpwebrequestwebrequest

解决方案


您只需添加:

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

"the secret key"您的 API 密钥在哪里。


推荐阅读