.Net 发起post 请求 并附加Authorization
时间: 2023-03-01 16:35:50 浏览: 72
.NET provides multiple ways to perform a POST request and attach an "Authorization" header. Here's an example using the `HttpClient` class in .NET:
```
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace HttpClientExample
{
class Program
{
static async Task Main(string[] args)
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer your-token-here");
var content = new StringContent("{\"key\":\"value\"}", Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://your-api-endpoint.com", content);
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}
```
In the above example, we create an instance of the `HttpClient` class and add an "Authorization" header with the value "Bearer your-token-here". Then, we create a `StringContent` object that represents the request payload, in this case a JSON string. Finally, we perform the POST request by calling `PostAsync` on the `HttpClient` instance and passing it the API endpoint URL and the `StringContent` object.
Note: Replace "your-token-here" with your actual authorization token and "https://your-api-endpoint.com" with the URL of the API endpoint you want to call.