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
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01