简体   繁体   English

使用HttpClient的多个自定义请求标头条目

[英]multiple custom request header entries using HttpClient

I would like to have multiple custom request header entries for my httpclient object before I send the request. 发送请求之前,我想为httpclient对象创建多个自定义请求标头条目。 The Request I want to send should look like this: 我要发送的请求应如下所示:

Request Headers     
Connection:keep-alive
Content-Length:213
Content-Type:application/json     
Host:portal.idtbeyond.com
Origin:https://portal.idtbeyond.com
Referer:https://portal.idtbeyond.com/activedocs
x-idt-beyond-app-id:xxccccxx
x-idt-beyond-app-key:yyuuuttttddfdfdfdfdfd
X-Requested-With:XMLHttpRequest
Request Payload
view parsed
{
   "country_code": "SV",
   "carrier_code": "Claro",
   "mobile_number": "50363751234",
   "plan": "Sandbox",
   "amount": "500",
   "client_transaction_id": "",
   "terminal_id": "KIOSK 1",
   "origin_country": "US"
}

The Code I am writing in C# is as below: 我用C#编写的代码如下:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(_ProviderService.GatewayUrl);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(
        new  MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Add("x-idt-beyond-app-id",
       _PayingMerchant.Username);
    client.DefaultRequestHeaders.Add("x-idt-beyond-app-key", 
       _PayingMerchant.Password);
    var urlSuffix = GetUrlSuffix(trans);                  

    HttpResponseMessage response = client.GetAsync(urlSuffix).Result;
    if (response.IsSuccessStatusCode)
        {
            TopUpResponse topUpResponse = 
               response.Content.ReadAsAsync<TopUpResponse>().Result;
            if (topUpResponse.success)
            {
                result.Success = true;
                result.ReturnValue = true;
            }
        }
    }

I am getting a 404 response and debugging shows that the header section is wrong on my part. 我收到404响应,并且调试显示我的标头部分错误。 I am new to these requests and would appreciate any help. 我是这些要求的新手,不胜感激。

Regards 问候

Try setting referer as well as follows: 尝试设置引荐来源网址,如下所示:

client.DefaultRequestHeaders.Referer = https://portal.idtbeyond.com/activedocs

And make sure you are using correct GatewayUrl 并确保您使用正确的GatewayUrl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM