【问题标题】:How to add Token into HTTP Post method in c#?如何在 c# 中将 Token 添加到 HTTP Post 方法中?
【发布时间】:2019-03-20 14:10:01
【问题描述】:

大家好,这是我的任务方法,它是http post方法,现在我想添加它像Token一样

httpWebRequest.Headers["Authorization"] = "Some_Token";

所以,我是新手,不知道该怎么做,有人可以帮助我吗?

            Task t = Task.Run(async () =>
            {
                string GetReferralsByPersonalIdNumberURL = "http://somehost/api/Referral/GetExistingClaimsByReferralNumber";
                GetExistingClaimsByReferralNumberClass cust = new GetExistingClaimsByReferralNumberClass() { referralNumber = ReferalNumFromMedicine, pharmacyID = PharmacyIDFromMedicine, };
                var json = _Serializer.Serialize(cust);
                var response = await Request(HttpMethod.Post, GetReferralsByPersonalIdNumberURL, json, new Dictionary<string, string>());

               //  Request.Headers["Authorization"] = "SomeToken"; 
                string responseText = await response.Content.ReadAsStringAsync();

                da = convertJsonStringToDataset(responseText);
                // List<YourCustomClassModel> serializedResult = _Serializer.Deserialize<List<YourCustomClassModel>>(responseText);

                //  Console.WriteLine(da.GetXml());
                // Console.ReadLine();

            });
            t.Wait();

这是 [webmethod] 中使用的request 方法

static async Task<HttpResponseMessage> Request(HttpMethod pMethod, string pUrl, string pJsonContent, Dictionary<string, string> pHeaders)
        {
            var httpRequestMessage = new HttpRequestMessage();
            httpRequestMessage.Method = pMethod;
            httpRequestMessage.RequestUri = new Uri(pUrl);
            foreach (var head in pHeaders)
            {
                httpRequestMessage.Headers.Add(head.Key, head.Value);
            }
            switch (pMethod.Method)
            {
                case "POST":
                    HttpContent httpContent = new StringContent(pJsonContent, Encoding.UTF8, "application/json");
                    httpRequestMessage.Content = httpContent;
                    break;

            }

            return await _Client.SendAsync(httpRequestMessage);
        }

我认为我必须更改内部请求方法,并且我认为我以另一种方式做到了,希望你们能理解我在尝试什么

【问题讨论】:

  • 请添加Request方法码
  • 这个Request 类是从哪里导入的?
  • 我编辑了,这是我的全部方法,你需要吗?
  • 你有这行await Request(HttpMethod.Post, GetReferralsByPersonalIdNumberURL, json, new Dictionary&lt;string, string&gt;()); 。请提供此Request 方法中的代码
  • 做这样的事情httpRequestMessage.Headers.Add("Authorization", $"Bearer {token}");

标签: c# http-post token


【解决方案1】:

像这样拨打Request

var headers = new Dictionary<string, string> {
    { "Authorization", "SomeToken" }
};

var response = await Request(HttpMethod.Post, GetReferralsByPersonalIdNumberURL, json, headers);

【讨论】:

  • 坦克你,它调试成功
【解决方案2】:

像这样添加一个新的授权标头,就像您在循环中所做的那样

 httpRequestMessage.Headers.Add("Authorization", $"Bearer {token}");

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 2022-11-17
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2011-03-18
    • 2021-10-01
    • 2022-06-17
    • 1970-01-01
    相关资源
    最近更新 更多