因为项目需求,需要获取 http request 请求中 header 里面一个叫 Authorization 参数的值,并做转发到第三方的API,正常来说使用
获取是

var authorization =  Request.Headers.GetValues("Authorization");

但是需要注意,因为 http 协议头只支持 ASCII 编码,如果需要传中文必须进行 UrlEncode ,否则在程序中将获取不到该参数,如下图

然后发送请求带上 header 则是

public static string PostRequest(string url, string param)
{
    WebHeaderCollection header = new WebHeaderCollection();
    header.Add("Authorization", "xxx");

    string strValue = ""; 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.KeepAlive = false;
    request.Method = "POST";
    request.ContentType = "application/json";

    request.Headers.Add(header);
    
    string paraUrlCoded = param;
    byte[] payload;
    payload = Encoding.UTF8.GetBytes(paraUrlCoded);
    request.ContentLength = payload.Length;
    Stream writer = request.GetRequestStream();
    writer.Write(payload, 0, payload.Length);
    writer.Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    Stream s = response.GetResponseStream();
    string StrDate = "";

    StreamReader Reader = new StreamReader(s, Encoding.UTF8);
    while ((StrDate = Reader.ReadLine()) != null)
    {
        strValue += StrDate + "\r\n";
    }
    return strValue;
}