c# - 设置 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";
...
解决方案
您只需添加:
request.Headers.Add("x-api-key", "the secret key");
"the secret key"
您的 API 密钥在哪里。
推荐阅读
- javascript - 无法在尚未安装的组件上调用 setState - tsx
- node.js - bookshelf.js – 如何获取空查询或根本没有查询的所有记录?
- c - 无法使用 sqlite3_bind_text 插入数据库
- c++ - 如何正确计算浮点对总和
- c++ - 带有 const 成员的对向量
- android - 如何解决短信权限的谷歌播放策略?
- c# - c# 使用两个 MysqlReader
- php - 如何从内容部分创建数组
- gcc - Mingw gcc,“-shared -static”一起传递
- vba - VBA:是否可以对别名函数名称进行编码/解码(别名“CreateFileW”-> Alias Enc(182893))