简体   繁体   English

Azure 功能 / API:如何创建代理

[英]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将鼠标悬停在“代理”上告诉我:“此功能不适用于 V4 功能应用

在此处输入图像描述

I found this , which suggests using API Management instead.我发现了这个,它建议使用 API Management 代替。 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":使用右侧的后端配置,如果我覆盖“服务 URL”,我可以将请求转发到特定的 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.有/曾经有一个名为 Azure 函数代理的功能。 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.如果您只想为其他 API 调用提供接口,我会推荐 APIM。 But you should be able to return what ever result you want with Functions also, but you need to implement it completely yourself.但是您也应该能够使用 Functions 返回您想要的任何结果,但是您需要自己完全实现它。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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