大家好,这是我的任务方法,它是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
var headers = new Dictionary<string, string> {
{ "Authorization", "SomeToken" }
};
var response = await Request(HttpMethod.Post, GetReferralsByPersonalIdNumberURL, json, headers);
正如您在循环中所做的那样添加这样的新Authorization标头
httpRequestMessage.Headers.Add("Authorization", $"Bearer {token}");