如何使用不记名令牌调用 HttpClient PostAsync 或 SendAsync 以获取带有 XML 参数的 XML?

问题描述 投票:0回答:1

到目前为止,使用不记名令牌调用带有 XML 参数的 XML 已通过 RestSharp 完成 在哪里

XElement Payload = <Access><AppID>35</AppID><UserToken>QrEil9L9S</UserToken></Access>

 public string PostFromRest(string API_URL, string subURL, string bearerToken, XElement Payload)
 {

  var client = new RestClient(API_URL);
  var request = new RestRequest(subURL, Method.Post);
  request.AddHeader("Cache-Control", "no-cache");
  request.AddHeader("Content-Type", "application/xml");
  request.AddHeader("Accept", "application/xml");
  request.AddHeader("messageType", "REQUEST");
  request.AddHeader("Authorization", bearerToken);
  request.AddHeader("messageId", Guid.NewGuid().ToString());
  request.AddHeader("timestamp", DateTime.Now.ToString());
  request.AddXmlBody(Payload, ContentType.Xml);
  var response = client.Execute(request);
  return response.Content;
}

我需要对 HttpClient 做同样的事情。 我正在尝试:

HttpClient client = new HttpClient())
client.DefaultRequestHeaders.Clear();
client.BaseAddress = new Uri(API_URL);
client.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Authorization", bearerToken);
var content1 = new FormUrlEncodedContent(
new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("Cache-Control", "no-cache"),
new KeyValuePair<string, string>("Content-Type", "application/xml"),
new KeyValuePair<string, string>("Accept", "application/xml"),
new KeyValuePair<string, string>("messageType", "REQUEST"),
new KeyValuePair<string, string>("messageId", Guid.NewGuid().ToString()),
new KeyValuePair<string, string>("timestamp", DateTime.Now.ToString())});

HttpResponseMessage response = client.PostAsync(subURL, content).Result;

但是认证失败。 我试图使用 HttpRequestMessage:

    using (var request = new HttpRequestMessage(HttpMethod.Post, API_URL))
{
  request.Headers.Add("Cache-Control", "no-cache");
  request.Headers.Add("Content-Type", "application/xml");
  request.Headers.Add("Accept", "application/xml");
  request.Headers.Add("messageType", "REQUEST");
  request.Headers.Add("Authorization", bearerToken);
  request.Headers.Add("messageId", Guid.NewGuid().ToString());
  request.Headers.Add("timestamp", DateTime.Now.ToString());

  request.Content = new StringContent(Payload.ToString(), Encoding.UTF8, "application/xml");
  HttpResponseMessage response = client.SendAsync(request).Result;
}

但是授权也失败。 如何使用不记名令牌调用 HttpClient 以获取带有 XML 参数的 XML?

c# xml token dotnet-httpclient restsharp
1个回答
0
投票

创建一个

HttpClient
实例并设置其
BaseAddress

var client = new HttpClient() { BaseAddress = new Uri(API_URL) };

创建一个

HttpRequestMessage
并设置相对 URL 和动词

var request = new HttpRequestMessage(HttpMethod.Post, new Uri(subURL));

设置请求标头

  • 我建议先设置标准的,然后设置自定义的
//Standard headers
request.Headers.Add("Cache-Control", "no-cache");
request.Headers.Add("Accept", "application/xml");
request.Headers.Add("Authorization", bearerToken);

//Custom headers
request.Headers.Add("messageType", "REQUEST");
request.Headers.Add("messageId", Guid.NewGuid().ToString());
request.Headers.Add("timestamp", DateTime.Now.ToString());

创建并设置请求正文

var content = new StringContent(Payload.ToString(), Encoding.UTF8, "application/xml");
request.Content = content;

发送请求并处理响应

var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
© www.soinside.com 2019 - 2024. All rights reserved.