'Azure function An attempt was made to access a socket in a way forbidden by its access permissions
I'm working with an azure function and calling a local API to test my function.
I keep getting this error:
An attempt was made to access a socket in a way forbidden by its access permissions.
The URL I'm trying to call looks something like https://localhost:xxxxx
I have seen a lot of other StackOverflow posts such as changing my app service plan and I'm using the standard and still get the same error.
Edit:
using (var client = new HttpClient())
{
using (var response = await client.GetAsync("http://localhost:xxxx"))
{
string responseData = await response.Content.ReadAsStringAsync();
}
}
Solution 1:[1]
One of the workaround I did to call the API from the Azure Function App (.NET 6) is below:
Function1.cs:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net;
namespace KrishNet6FuncHttpTrigger
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "GetServerVariables")] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var result = "";
#pragma warning disable SYSLIB0014 // Type or member is obsolete
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.sampleapis.com/coffee/hot");
#pragma warning restore SYSLIB0014 // Type or member is obsolete
httpWebRequest.ContentType = "application/json";
//httpWebRequest.Headers["Authorization"] = "Bearer **token**";
httpWebRequest.Method = "GET";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls;
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
log.LogInformation(result);
}
return new OkObjectResult(result);
}
}
}
Result:

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | HariKrishnaRajoli-MT |
