Azure Functions call http post inside function(Azure Functions 在函数内部调用 http post)
问题描述
是否可以在 Azure Function 中创建 HTTP(s) 发布请求?我正在尝试创建一个自定义 webhook,它正在侦听一项服务,并在触发时使用 post 通过 HTTP 调用另一项服务.
Is it possible to create HTTP(s) post request inside Azure Function? I am trying to create a custom webhook that is listening to one service and when triggered then its calling another service over HTTP using post.
我的代码是这样的:
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
BitbucketRequest data = await req.Content.ReadAsAsync<BitbucketRequest>();
//DO STH WITH DATA TO GET e.g. USER STORY ID
using(var client = new HttpClient()){
client.BaseAddress = new Uri("https://SOME_TARGETPROCESS_URL/api/v1");
var body = new { EntityState = new { Id = 174 } };
var result = await client.PostAsJsonAsync(
"/UserStories/7034/?resultFormat=json&access_token=MYACCESSTOKEN",
body);
string resultContent = await result.Content.ReadAsStringAsync();
}
return req.CreateResponse<string>(HttpStatusCode.OK,"OKOK");
}
我想问题是当前 HttpRequestMessage 正在占用 web socket,我无法创建新的 Http Request.
I suppose the problem is that currently HttpRequestMessage is occupying web socket and I can not create new Http Request.
我在异常详情中发现的错误:
Errors that I found in Exceptions details:
- 底层连接已关闭:发送时发生意外错误.
- 无法从传输连接读取数据:现有连接被远程主机强行关闭.
- 套接字异常错误代码:10054
推荐答案
适用于在 Azure 函数中搜索 HttpClient 时登陆此处的其他任何人.
For anyone else who lands here when searching for HttpClient in Azure functions.
https://docs.microsoft.com/en-我们/azure/azure-functions/manage-connections
// Create a single, static HttpClient
private static HttpClient httpClient = new HttpClient();
public static async Task Run(string input)
{
var response = await httpClient.GetAsync("https://example.com");
// Rest of function
}
这篇关于Azure Functions 在函数内部调用 http post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Azure Functions 在函数内部调用 http post


- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01