Azure 函数在函数内部调用 http post [英] Azure Functions call http post inside function

查看:24
本文介绍了Azure 函数在函数内部调用 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 正在占用网络套接字,我无法创建新的 Http 请求.

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 函数在函数内部调用 http post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆