[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