Azure Functions在函数内部调用http发布

Prz*_*icz 7 .net sockets azure dotnet-httpclient azure-functions

是否可以在Azure Function中创建HTTP发布请求?我正在尝试创建一个自定义的Webhook,该Webhook正在侦听一个服务,并在触发时随后使用post通过HTTP调用另一个服务。

我的代码如下所示:

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");
}
Run Code Online (Sandbox Code Playgroud)

我想问题是当前HttpRequestMessage正在占用Web套接字,并且我无法创建新的Http请求。

我在“异常”详细信息中发现的错误:

  • 基础连接已关闭:发送时发生意外错误。
  • 无法从传输连接中读取数据:远程主机强行关闭了现有连接。
  • 套接字异常错误代码:10054

Rya*_* E. 13

对于在 Azure 函数中搜索 HttpClient 时到达这里的任何其他人。

https://docs.microsoft.com/en-us/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
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*kov 6

当然有可能,下面的代码块在我的测试函数中也可以正常工作:

using(var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://www.google.com");
    var result = await client.GetAsync("");
    string resultContent = await result.Content.ReadAsStringAsync();
    log.Info(resultContent);
}
Run Code Online (Sandbox Code Playgroud)

它输出google.com的HTML。POST也可以使用:从Google返回错误405(不允许使用方法)!! 1。

可能是您的被呼叫者失败了吗?

  • 值得指出的是,在函数中使用HttpClient时,您应该注意此反模式。https://docs.microsoft.com/zh-cn/azure/architecture/antipatterns/improper-instantiation/ (11认同)

4c7*_*b41 5

我已经在 Azure Function 中完成了 HTTP 帖子,如下所示:

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, string arg1, string arg2, string arg3, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");
    var text = String.Format("arg1: {0}\narg2: {1}\narg3: {2}", arg1, arg2, arg3);
    log.Info(text);

    var results = await SendTelegramMessage(text);
    log.Info(String.Format("{0}", results));

    return req.CreateResponse(HttpStatusCode.OK);
}

public static async Task<string> SendTelegramMessage(string text)
{
    using (var client = new HttpClient())
    {

        Dictionary<string, string> dictionary = new Dictionary<string, string>();
        dictionary.Add("PARAM1", "VALUE1");
        dictionary.Add("PARAM2", text);

        string json = JsonConvert.SerializeObject(dictionary);
        var requestData = new StringContent(json, Encoding.UTF8, "application/json");

        var response = await client.PostAsync(String.Format("url"), requestData);
        var result = await response.Content.ReadAsStringAsync();

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您从名称中猜到的那样,我正在使用它向电报机器人发送 POST 请求