Fowarding HTTP request to an external URL

杨岑 176 Reputation points
2022-12-17T15:03:43.423+00:00

Is it possible to build an ASP.NET Core page to forward all HTTP request (query string/headers/post data) to an external URL and then captures response and send it back to client?

For example:

  1. I make request (HttpRequest) to http://www.mydomain.com/handler.aspx?a=1&b=2
  2. Handler.apsx forwards the request to http://www.somecompnay.com/service
  3. Handler.aspx reads response from the external service
  4. Handler sends the response to my side which can be read by HttpResponse
Developer technologies | ASP.NET | ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-12-19T02:57:17.233+00:00

    Hi @杨岑 ,

    Is it possible to build an ASP.NET Core page to forward all HTTP request (query string/headers/post data) to an external URL and then captures response and send it back to client?

    In Asp.net core, you can write a custom ASP.NET Core middleware to filter the request (use the Map, and Use extension methods), then use the HttpContext.Request.IsHttps property to verify whether the request is a http request or https request. After that based on the result to invoke the middleware or call the external method and make a custom response.

    Code like this:

    app.Use(async (context, next) =>  
    {  
        var ishttps = context.Request.IsHttps;   
        var razprpage = context.Request.Path.ToString().StartsWith("/");  
        if (ishttps && razprpage)  
        {  
            await context.Response.WriteAsync($"Current request is a Https request");   
            //// Call the next delegate/middleware in the pipeline.  
            //await next(context);  
            await next(context);  
        }  
        else  
        {  
            //call the external sevice and get the response.   
            using (HttpClient client = new HttpClient())  
            {  
                //HttpResponseMessage response = await client.GetAsync($"external url");  
                //string result = await response.Content.ReadAsStringAsync();  
      
                await context.Response.WriteAsync($"Current request is not a Https request");  
                //await context.Response.WriteAsync(result);  
            }  
      
        }  
    });  
      
    app.UseHttpsRedirection();  
    

    [Note] By default, Asp.net core enforce HTTPS using the UseHttpsRedirection middleware, so, if you want to check request url via the above method, you should add the custom middleware before the UseHttpsRedirection middleware. Or you can remove the UseHttpsRedirection middleware.

    The result as below:

    271869-image.png

    More detail information about custom Middleware, see:

    Write custom ASP.NET Core middleware

    ASP.NET Core Middleware


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.