首页 > 编程语言 >C#通用代理-http请求转发

C#通用代理-http请求转发

时间:2022-10-25 11:02:54浏览次数:38  
标签:http header C# Headers requestMessage 转发 var new Response

[HttpPost]
[HttpGet]
[Route("OaProxy/{*uri}")]
[DontWrapResult(WrapOnError = false, WrapOnSuccess = false, LogError = true)]
public async Task OaProxy(string uri)
{
    var queryString = HttpUtility.ParseQueryString(Request.QueryString.Value);
    queryString.Add("key", AppConsts.oaKey);
    var requestUri = new Uri(String.Format("{0}?{1}", uri, queryString), UriKind.Relative);
    var requestMessage = new HttpRequestMessage();
    requestMessage.RequestUri = requestUri;
    requestMessage.Method = new HttpMethod(Request.Method);

    if (requestMessage.Method == HttpMethod.Post)
    {
        //requestMessage.Content = new StreamContent(Request.Body);
        var formContent = new List<KeyValuePair<string, string>>();
        foreach (var formKey in Request.Form.Keys)
        {
            var content = Request.Form[formKey].FirstOrDefault();
            formContent.Add(new KeyValuePair<string, string>(formKey, content));
        }
        requestMessage.Content = new FormUrlEncodedContent(formContent);
    }

    var client = _httpClientFactory.CreateClient("oa");
    var response = await client.SendAsync(requestMessage);

    Response.StatusCode = (int)response.StatusCode;
    foreach (var header in response.Headers)
    {
        Response.Headers[header.Key] = header.Value.First();
    }
    foreach (var header in response.Content.Headers)
    {
        Response.Headers[header.Key] = header.Value.First();
    }
    Response.Headers.Remove("transfer-encoding");
    Response.Headers.Remove("Content-Type");
    Response.Headers.Add("Content-Type", "application/javascript");
    await response.Content.CopyToAsync(Response.Body);
    Response.Body.Flush();
    Response.Body.Close();
}

标签:http,header,C#,Headers,requestMessage,转发,var,new,Response
From: https://www.cnblogs.com/itjeff/p/16824148.html

相关文章

  • jenkin部署(docker+gitlab)
    第一部分安装部署1.1gitlab安装部署最小配置内存至少4g,系统内核至少在3.10以上uname-r可查看系统内核版本默认docker已经安装1、安装gitlab127机器(1)添加容器......
  • CF1709E
    设\(val_u\)表示树中\(1\)到\(u\)路径上的点权异或和。那么\(u\)到\(v\)路径上的点权异或和为\(0\)说明\(val_u\oplusval_v\oplusa_{\text{lca}(u,v)}=0\)......
  • CF 265B(行道树简化版)
    B.RoadsideTrees(SimplifiedEdition)timelimitpertestmemorylimitpertestinput......
  • SpringCloud-04 Feign学习笔记
    @​​TOC​​一、什么是Feign?Feign是声明式WebService客户端,它让微服务之间的调用变得更简单,类似controller调用service。SpringCloud集成了Ribbon和Eureka,可以使用Feigin......
  • mysqldump: Got error: 2002: "Can't connect to local MySQL server through socket
    netstat-ln|grepmysql查看mysql.sock实际路径:[root@localhostbackup]#netstat-ln|grepmysqlunix2[ACC]STREAMLISTENING62019......
  • SpringCloud-05 Hystrix学习笔记
    @[Toc]一、Hystrix简介1、Hystrix是什么?流量高峰时,一个单节点的宕机或延迟,会迅速导致所有服务负载达到饱和。应用中任何一个可能通过网络访问其他服务的节点,都有可能成为......
  • pycharm中使用pip报错:You should consider upgrading via the 'python -m pip install
    问题:在pycharm中使用pip命令安装时出现以下报错:解决办法:1.在python安装路径下找到site-packages文件夹下的类似pip-21.1.3.dist-info文件夹,将整个文件夹删掉 2.再在p......
  • www.cnblogs.com/huakexiaopeng
    对于这个问题,我觉得要看业务而定。我们都知道,独享HTTP代理池是一个人使用的IP池,纯净度极高,当然价格也更贵;共享HTTP代理池是很多用户同时在IP池里获取HTTP代理使用,价格......
  • Asp.Net MVC路由
    一、普通路由1、修改Controller路由[RoutePrefix("news")]//修改Controller路由publicclassProductController:Controller2、修改act......
  • efcore 连接SqlServer2008R2报错:'OFFSET' 附近有语法错误。 在 FETCH 语句中选项 NEXT
    用的是EFCore6,连接SqlServer2008R2时,生成的分页方法会报错,只需要指定ProviderName时加上版本号就行:Microsoft.EntityFrameworkCore.SqlServer@2008,高于2008版本就按默......