简体   繁体   中英

How do asp.net core universal proxy

I`m do proxy service in asp.net core, extensions where the request will be routed to another service.

My extension:

 public static IApplicationBuilder UseProxy(this IApplicationBuilder builder, List<ProxySetting> options)
    {
        builder.Use(async (context, next) =>
        {
            var resultPathHandler = options.ProxySettingHandler(context.Request.Path, context.Request.Method);

            if (!resultPathHandler.Equal)
                //continues through the rest of the pipeline
                await next();
            else
            {
                using (var httpClient = new HttpClient())
                {
                    var pathSetting = resultPathHandler.Setting;
                    var requestMessage = new HttpRequestMessage();

                    //добавляем хедеры
                    foreach (var header in context.Request.Headers)
                    {
                        if (!httpClient.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value.ToArray()))
                        {
                            httpClient.DefaultRequestHeaders.TryAddWithoutValidation(header.Key,
                                header.Value.ToArray());
                        }
                    }


                    if (context.Request.HasFormContentType && context.Request.Form.Count > 0)
                    {
                        var temp = new Dictionary<string, object>();
                        foreach (var form in context.Request.Form)
                        {
                            requestMessage.Properties.Add(form.Key, form.Value);
                            temp.Add(form.Key, form.Value);
                        }
                        var jsonString = JsonConvert.SerializeObject(temp).Replace(":[", ":").Replace("],",",");

                        var mediatype = string.Empty;
                        if (context.Request.ContentType.Contains(';'))
                            mediatype = context.Request.ContentType.Split(';')[0];
                        else
                            mediatype = context.Request.ContentType;

                        requestMessage.Content = new StringContent(jsonString, Encoding.UTF8, mediatype);
                    }

                    requestMessage.Headers.Host = pathSetting.Host;
                    var host = pathSetting.GetUri(context.Request.Path);
                    var uriString = $"{host}{context.Request.QueryString}";
                    requestMessage.RequestUri = new Uri(uriString);
                    requestMessage.Method = new HttpMethod(pathSetting.Method);
                    var response = await httpClient.SendAsync(requestMessage);
                    var result = await response.Content.ReadAsStringAsync();
                    var resultObj = JsonConvert.DeserializeObject<object>(result);
                    context.Response.StatusCode = (int)response.StatusCode;
                    await context.Response.WriteAsync(resultObj.ToString());
                }
            }
        });

        return builder;
    }

where this code:

                if (context.Request.HasFormContentType && context.Request.Form.Count > 0)
                {
                    var temp = new Dictionary<string, object>();
                    foreach (var form in context.Request.Form)
                    {
                        requestMessage.Properties.Add(form.Key, form.Value);
                        temp.Add(form.Key, form.Value);
                    }
                    var jsonString = JsonConvert.SerializeObject(temp).Replace(":[", ":").Replace("],",",");

                    var mediatype = string.Empty;
                    if (context.Request.ContentType.Contains(';'))
                        mediatype = context.Request.ContentType.Split(';')[0];
                    else
                        mediatype = context.Request.ContentType;

                    requestMessage.Content = new StringContent(jsonString, Encoding.UTF8, mediatype);
                }

Add to content post and put parameters, with application/x-www-form-urlencoded or application/form-data, and send to other service, but i dont get this parameters in other service. Help me, need release this feature. Thx and sorry for my bad English

You don't need to to reinvent the wheel. ASP.NET Core already has and provides own ProxyMiddleware .

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