zoukankan      html  css  js  c++  java
  • 对http请求进行过滤处理,转换成接收着需要的格式

     
    需要在Global.asax的Application中进行初始化处理 这样:GlobalConfiguration.Configuration.MessageHandlers.Add(new DefaultHandler())
    然后在App_Start文件夹中添加 DefaultHandler 类复制下面的代码
    public class DefaultHandler : DelegatingHandler
    {
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
    request.RequestUri = new Uri(request.RequestUri.ToString());
    
    MediaTypeHeaderValue contentType = request.Content.Headers.ContentType;
    
    if (contentType != null)
    {
    switch (contentType.MediaType)
    {
    case "application/x-www-form-urlencoded":
    {
    NameValueCollection formData = await request.Content.ReadAsFormDataAsync(cancellationToken);
    request.Content = new FormUrlEncodedContent(Correct(formData));
    
    //如下代码为了预下订单使用
    var formDataDic = formData.ToDictionary();
    var str = ConvertToJsonString(formDataDic);
    HttpContext.Current.Request.Headers.Add("formDataToJson", str);
    }
    break;
    case "multipart/form-data":
    {
    NameValueCollection formData = await request.Content.ReadAsFormDataAsync(cancellationToken);
    request.Content = new FormUrlEncodedContent(Correct(formData));
    }
    break;
    case "application/json":
    {
    HttpContentHeaders oldHeaders = request.Content.Headers;
    string formData = await request.Content.ReadAsStringAsync();
    request.Content = new StringContent(formData);
    ReplaceHeaders(request.Content.Headers, oldHeaders);
    }
    break;
    default:
    throw new Exception("Implement It!");
    }
    }
    
    return await base.SendAsync(request, cancellationToken);
    }
    
    private static IEnumerable<KeyValuePair<string, string>> Correct(NameValueCollection formData)
    {
    return formData.Keys.Cast<string>().Select(key => new KeyValuePair<string, string>(key, formData[key])).ToList();
    }
    
    private static void ReplaceHeaders(HttpHeaders currentHeaders, HttpHeaders oldHeaders)
    {
    currentHeaders.Clear();
    foreach (var item in oldHeaders)
    currentHeaders.Add(item.Key, item.Value);
    }
    
    private string ConvertToJsonString(IDictionary<string, string> param)
    {
    dynamic myObj = new ExpandoObject();
    var dic = (IDictionary<string, object>)myObj;
    foreach (var item in param)
    {
    if (item.Value.StartsWith("[") || item.Value.StartsWith("{"))
    {
    dic[item.Key] = JsonConvert.DeserializeObject(item.Value);
    }
    else
    {
    dic[item.Key] = item.Value;
    }
    }
    return JsonConvert.SerializeObject(myObj);
    }
    }
    public static class NVCExtender
    {
    public static IDictionary<string, string> ToDictionary(this NameValueCollection source)
    {
    
    return source.AllKeys.ToDictionary(k => k, k => source[k]);
    
    }
    }
  • 相关阅读:
    第七节:Asp.Net Core内置日志、将EF生成的SQL输出到控制台
    自学Zabbix3.6.2-触发器triggers severity严重程度
    自学Zabbix3.6.1-触发器triggers创建
    自学Zabbix3.5.7-监控项item-Applications
    自学Zabbix3.5.6-监控项item-Value mapping值映射
    自学Zabbix3.5.5-监控项item-User parameters(自定义key)
    自学Zabbix3.5.4-监控项item-History and trends
    自学Zabbix3.5.3-监控项item-zabbix agent 类型所有key
    自学Zabbix3.5.2-监控项item-types监控类型
    自学Zabbix3.5.1-监控项item-key介绍
  • 原文地址:https://www.cnblogs.com/sunqiang/p/7286591.html
Copyright © 2011-2022 走看看