简体   繁体   中英

Azure functions / API: How to create a proxy

I am trying to create a proxy app that will forward my requests.

I will use it like this (works for some public proxies):

var proxy = new WebProxy { Address = new Uri("proxyUrl") };
var httpClientHandler = new HttpClientHandler { Proxy = proxy };
var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);
await client.GetAsync("https://www.whatsmyip.org");

I found this and this guide by microsoft, however they seem to be outdated, since functions can't have proxies anymore. Hovering over "Proxies" tells me: "This feature is not available for V4-function-apps

在此处输入图像描述

I found this , which suggests using API Management instead. So I tried. It looks like this: 在此处输入图像描述

Using the backend configuration on the right, I can forward the request to a specific url if I override the "Service URL": 在此处输入图像描述

But I want to use it as shown in my first code snippet, as a real proxy that works for any endpoint. Any ideas on that?

There is/was a feature called Azure Functions Proxy. This was used to redirect/modify request. However, even if this feature is gone now, it doesn't mean you can't implement something which redirects you to another website.

[FunctionName(nameof(Redirect))]
public static IActionResult Redirect(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "redirect")] HttpRequest req,
    ILogger log)
{   
        return new RedirectResult("https://google.com", true);
}

If you want just to provide an interface for other API calls, I would recommend APIM. But you should be able to return what ever result you want with Functions also, but you need to implement it completely yourself.

It could look something like this (not tested)

[FunctionName(nameof(RemoteAPICall))]
public static IActionResult RemoteAPICall(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "proxy")] HttpRequest req,
    ILogger log)
{   
    var response = await client.GetAsync("http://www.wtfismyip.com/json");
    return new OkObjectResult(response.Content);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM