Azure Functions 在函数内部调用 http post

Azure Functions call http post inside function(Azure Functions 在函数内部调用 http post)
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)