ASP.NET Web API发布到外部API

发布于 2025-02-04 04:28:34 字数 99 浏览 4 评论 0原文

我想问一个创建的ASP.NET Web API(用C#编写)发布到外部API是否可以?

如果可能的话,请共享可以通过添加标头发布到URL并从外部API接收回调的示例代码。

I would like to ask if it is possible for a created ASP.NET Web API (written in C#) to post to an external API?

If it is possible, please share sample code that can post to an url with adding headers and receive a callback from the external API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦妄 2025-02-11 04:28:34

从.net-application中使http-request的一种简单方法是system.net.http.httpclient msdn )。一个示例用法看起来像这样:

// Should be a static readonly field/property, wich is only instanciated once
var client = new HttpClient();

var requestData = new Dictionary<string, string>
{  
    { "field1", "Some data of the field" },
    { "field2", "Even more data" }
};

var request = new HttpRequestMessage() {
    RequestUri = new Uri("https://domain.top/route"),
    Method = HttpMethod.Post,
    Content = new FormUrlEncodedContent(requestData)
};

request.Headers // Add or modify headers

var response = await client.SendAsync(request);

// To read the response as string
var responseString = await response.Content.ReadAsStringAsync();

// To read the response as json
var responseJson = await response.Content.ReadAsAsync<ResponseObject>();

A simple way to make HTTP-Request out of a .NET-Application is the System.Net.Http.HttpClient (MSDN). An example usage would look something like this:

// Should be a static readonly field/property, wich is only instanciated once
var client = new HttpClient();

var requestData = new Dictionary<string, string>
{  
    { "field1", "Some data of the field" },
    { "field2", "Even more data" }
};

var request = new HttpRequestMessage() {
    RequestUri = new Uri("https://domain.top/route"),
    Method = HttpMethod.Post,
    Content = new FormUrlEncodedContent(requestData)
};

request.Headers // Add or modify headers

var response = await client.SendAsync(request);

// To read the response as string
var responseString = await response.Content.ReadAsStringAsync();

// To read the response as json
var responseJson = await response.Content.ReadAsAsync<ResponseObject>();
身边 2025-02-11 04:28:34

本质上,您需要使用httpclient的实例将httprequestMessage发送到端点。

这是一个将一些jsondata发布到someendPointUrl的示例:

var client = new HttpClient();

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

    request.Headers.Accept.Clear();
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
    
    request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");

    var response = await client.SendAsync(request, CancellationToken.None);

    var str = await response.Content.ReadAsStringAsync();

    if (response.StatusCode == HttpStatusCode.OK)
    {
        // handle your response
    } 
    else 
    {
        // or failed response ?
    }

Essentially you need use an instance of HttpClient to send an HttpRequestMessage to an endpoint.

Here is an example to post some jsonData to someEndPointUrl:

var client = new HttpClient();

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

    request.Headers.Accept.Clear();
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
    
    request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");

    var response = await client.SendAsync(request, CancellationToken.None);

    var str = await response.Content.ReadAsStringAsync();

    if (response.StatusCode == HttpStatusCode.OK)
    {
        // handle your response
    } 
    else 
    {
        // or failed response ?
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文