0

I have a HTTP Listener console app that works on my local machine. When I try to use it inside a HTTP Trigger Azure Function. I always get the 418 error code.

In my console app:

HttpListener listener = new HttpListener();
try
{
    listener.Prefixes.Add("http://localhost:11000/");
    listener.Start();
} catch (Exception e)
{ // }
do {
        var ctx = listener.GetContext();
        var res = ctx.Response;
        var req = ctx.Request;
        var reqUrl = req.Url;
        var readStream = new StreamReader(req.InputStream);
        var content = readStream.ReadToEnd();
        Console.WriteLine(content);
        // business logic
        readStream.Close();
        res.StatusCode = (int)HttpStatusCode.OK;
        res.ContentType = "text/plain";
        res.OutputStream.Write(new byte[] { }, 0, 0);
        res.Close();
        if (stopListener) { listener.Stop(); }
    } while (listener.IsListening);

Now HTTP Trigger Function uses the HttpRequest class and that seems to give me the 418 error code. I replaced it with HttpListener() but when I add the prefix of the Azure Function string connection (on the CLI), the stream never goes through and its as if its not capturing it? Or what connection should I use? I feel like self-referencing it is the reason its not working.

Azure Function:

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpListener listener,
            ILogger log,
            IBinder binder)
        {//same as above}

Is this the right approach to getting data from an external app? So far this has been the way I can see it working via the HTTP Listener.

Any suggestions are welcomed.

  • what are you trying to do ? why do you need ot use an httplistener ? – Thomas Jul 23 '22 at 20:55
  • Yes, I'm using an external app that sends data to my listener. It looks like HTTP Request wont work for the external app to capture the data and for me to transform it into a CSV file. It works locally, just not with Azure Functions it seems @Thomas – AnalyzingTasks Jul 23 '22 at 22:28

1 Answers1

1

Is this the right approach to getting data from an external app?

The right way to access Data from an external source and any other source. You can create an API and use this API to access data from external sources.

For create azure function click hereby Microsoft documents.

Below sample code for access web API in azure function.

  var _httpclient = new HttpClient(); 
  var _response = await _httpclient .GetAsync(rul); 
  var result_= await _response .Content.ReadAsStringAsync();

its use is just like using API in C# code.

Azure Function Code:-

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 System.Net.Http;

namespace _73093902_FunctionApp10
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var _httpclient = new HttpClient();
            var _response = await _httpclient.GetAsync("https://localhost:7101/WeatherForecast");
            var result_ = await _response.Content.ReadAsStringAsync();
            return new OkObjectResult(result_);
        }
    }
}

Debug Output:-

enter image description here

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15
  • Thank you for the detailed example @AjayKumarGhose-MT. That's what I tested on my end and it works with localhost data. But when I tried sending data from the external app to the HttpRequest trigger, I always receive 418 error code (teapot error). Testing it locally (console app), it seems that the external data source sends in multiple URLs with data and may be the reason that HttRequest is giving me that error (inside azure function). How would you use the HttpRequest in a console app? – AnalyzingTasks Jul 25 '22 at 12:22
  • Also I noticed that you're using the HttpClient (which wouldnt be the external data? Or would it be the other way around in this case?) instead of the HttpListener or HttpRequest/HttpListener in your Azure Function. I thought the HttpRequest/HttpListener would listen for POST data coming into the Function. – AnalyzingTasks Jul 25 '22 at 12:32
  • 1
    My result is also using external data source using and its other way on this https://stackoverflow.com/questions/10017564/url-mapping-with-c-sharp-httplistener ,these thread will help you on `HTTP listener`. – AjayKumarGhose Jul 25 '22 at 16:20
  • The thing about this external app is that I do not know the URL that its sending it from. For instance it has a field where you type the URL the data needs to go to. Locally with listener I type: http://10.0.x.x:1100/. With a HttpListener you can set it with Prefix.Add() function, with HttpClient, you need to know the URL it seems, The external app just sends a boatload of data for you to do something with it, so it seems that a listener is the approach. With Function would I need to do: GetAsync(req.Url) as I do not know the URL thats sending and hoping the HttpRequest is capturing it? – AnalyzingTasks Jul 25 '22 at 22:03
  • Also the example you provided, its string content, where as the request I'm getting, I need to handle InputStream, would that matter? And may be why I'm getting the 418 error code? – AnalyzingTasks Jul 25 '22 at 22:16