C# 验证HTTPClient和HTTPRequest代码

C# 验证HTTPClient和HTTPRequest代码,c#,json,httprequest,httpclient,soapui,C#,Json,Httprequest,Httpclient,Soapui,我正试图向身份验证公司发送请求。他们要求发送三个标题。我添加了如下标题: HttpClient client = new HttpClient(); client.BaseAddress = new Uri("https://api.test"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQua

我正试图向身份验证公司发送请求。他们要求发送三个标题。我添加了如下标题:

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("https://api.test");
                client.DefaultRequestHeaders.Accept.Add(new 
                MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
                client.DefaultRequestHeaders.TryAddWithoutValidation("hmac-signature", Hmackey);
                HttpRequestMessage request = new HttpRequestMessage();
                request.Method = HttpMethod.Post;

                request.Content = new StringContent(JSONFile, Encoding.UTF8,
                                    "application/json");
                HttpResponseMessage response = client.SendAsync(request).Result;
                string responseContent = await response.Content.ReadAsStringAsync();
Accept:application/json
Content-Type:application/json
hmac-signature:<TheCalculatedHMACSignature>
我在SOAP UI中发布了相同的JSON文件,并得到了回复,但当我使用上面的代码发送相同的JSON文件时,我得到了以下错误:

{
    "responseHeader": {
        "tenantID": "V2321",
        "requestType": "PreciseIdOnly",
        "clientReferenceId": "PIDInitial-2312313",
        "expRequestId": "",
        "messageTime": "2020-06-09T17:17:49Z",
        "responseCode": "R0302",
        "responseType": "ERROR",
        "responseMessage": "The HMAC validation failed : Invalid Signature or no matching alias found."
    },
    "originalRequestData": null
}
唯一的区别是,在我的代码中,我有“/r/n”和“/”。我可以删除“/r/n”,但不能删除“/”,因为双代号前面有正斜杠

我没有计算HMAC密钥,而是从HTTP日志中的SOAPUI获取HMAC密钥,并将其放入代码中。我仍然收到同样的错误,说“HMAC验证失败”。我只是想确保上面的代码看起来正确吗?我应该添加三个标题,如下所示:

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("https://api.test");
                client.DefaultRequestHeaders.Accept.Add(new 
                MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
                client.DefaultRequestHeaders.TryAddWithoutValidation("hmac-signature", Hmackey);
                HttpRequestMessage request = new HttpRequestMessage();
                request.Method = HttpMethod.Post;

                request.Content = new StringContent(JSONFile, Encoding.UTF8,
                                    "application/json");
                HttpResponseMessage response = client.SendAsync(request).Result;
                string responseContent = await response.Content.ReadAsStringAsync();
Accept:application/json
Content-Type:application/json
hmac-signature:<TheCalculatedHMACSignature>
Accept:application/json
内容类型:application/json
hmac签名:
我的代码是否与这三个标题匹配。这是我发送请求的第一个应用程序,我想可能是代码中的某些内容导致了这个错误。我不再计算HMAC密钥。我从soapui的Http日志中得到了密钥,soapui的响应看起来不错


任何验证我的代码的帮助都将不胜感激。

双代码前面有正斜杠。
这是什么意思?我的部分JSOn看起来是这样的:{\r\n\“header\”:{\r\n\“tenantId\”:\“V12345\,\r\n\“requestType\”:“precisedonly\”,\r\n\“clientReferenceId\”:“PIDInitial-12345\”如果您看到tenantID,它前面有反斜杠为什么需要这些斜杠?json字符串是否在代码中的某个地方硬编码?我想,C#在有引号的情况下放置斜杠。我使用类生成json文件,结果json中有斜杠和“/r/n”。我可以替换“/r/n”使用空字符串,但斜杠不会消失。我认为双引号是C#中的转义字符,可能这就是您需要的: