如何使用具有完全定义名称的 HttpRequestMessage 在 C# (Xamarin) 中发送自定义 header?

How to send custom header in C# (Xamarin) with HttpRequestMessage with the exactly defined names?

我需要将带有自定义 headers 的 Http 请求发送到在 PHP 上运行的服务器代码。

我为此使用了以下代码:

preparedAddress 是字符串 url; AuthenticationLine 和 dateValue 也是定义的字符串值。 'json' 变量包含准备好的字符串

代码如下:

client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
int _TimeoutSec = 90;
client.Timeout = new TimeSpan(0, 0, _TimeoutSec);
string _ContentType = "application/json";
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_ContentType));
var uri = new Uri(String.Format(preparedAddress));
System.Net.Http.HttpRequestMessage msgToSend = new HttpRequestMessage(HttpMethod.Post, uri);
//my custom headers
        msgToSend.Headers.Add("SN-Authentication", AuthenticationLine);
        msgToSend.Headers.Add("SN-Date", dateValue);
 var content = new StringContent(json, Encoding.UTF8, "application/json");
 content.Headers.ContentType = new MediaTypeHeaderValue(_ContentType);
 msgToSend.Content = content;
 HttpResponseMessage response;
        response = client.SendAsync(msgToSend).Result;
        String execResult = response.ReasonPhrase;

在php这边我获得了以下数据:

Array (     
[HTTP_SN_AUTHENTICATION] => Array ( [0] => my data 1 )      
[HTTP_SN_DATE] => Array ( [0] => my data 2 )

其他 header 未明确设置,但我不关心它们。

为什么 C# 更改了 header 名称?如何让 C# 发送 headers 我定义的名称?

问题出在我的 PHP 代码上, headers 处理过程中出现了一点错误。

感谢使用 Fiddler 工具的建议。我使用了类似的工具 - Postman,并且 PHP 以与从 C# 代码发送请求相同的格式显示 headers。 PHP 框架按预期识别和处理 headers。

结案...