问题描述
是否可以在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!