对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]); }
}
对http请求进行过滤处理,转换成接收着需要的格式的更多相关文章
- 带毫秒的字符转换成时间(DateTime)格式的通用方法
C#自身有更好的方式,Net任意String格式转换为DateTime类型 ====================================================== 原文 ==== ...
- 纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat)
纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat) 转载自:http://blog.cafeboy.org/2011/02/25/qqwry-to-ipwry/ ht ...
- Swift3 根据秒数获取视频时长(转换成00:00:00时间格式)以及将时长转换成秒
直接代码了: /// 秒转换成00:00:00格式 /// /// - Parameter secounds: <#secounds description#> /// - Returns ...
- 将日期转换为指定的格式:比如转换成 年月日时分秒 这种格式:yyyy-MM-dd hh:mm:ss 或者 yyyy-MM-dd。总结下。
可以为Date原型添加如下的方法: Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth() ...
- [转]Apache的CRT格式SSL证书转换成IIS用的PFX格式
转自:http://www.getvm.net/apache-crt-ssl-convert-to-iis-pfx/ Apache使用的SSL证书是.crt格式,如果你的网站从Apache换到了win ...
- 远程调用get和post请求 将返回结果转换成实体类
package org.springblade.desk.utils; import org.apache.http.client.ResponseHandler; import org.apache ...
- 结合Vim ghostscript 将源代码文件转换成语法高亮的pdf格式文档
step 1: 安装ghostscript (debian 环境, 其他环境自行google) sudo apt-get install ghostscript step 2: 用Vim生成ps文件 ...
- java把13位时间戳转换成"yyyy-MM-dd HH:mm:ss"格式,工具类
public static void main(String[] args) { String time = System.currentTimeMillis();//获取当前时间精确到毫秒级的时间戳 ...
- 把Java生成的RSA公钥、私钥转换成.NET使用的XML格式
import java.security.KeyFactory; import java.security.interfaces.RSAPrivateCrtKey; import java.secur ...
随机推荐
- C# 特性的使用
using ClassLibrary;using System;using System.Collections.Generic;using System.Linq;using System.Refl ...
- Win10如何关闭自动更新服务
原文:Win10如何关闭自动更新服务 第一步: 小娜搜索"gpedit.msc",进入本地计算机策略设置. 第二步: 找到策略位置:本地计算机策略-计算机配置-管理模板-Windo ...
- Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器
原文:Win8 Metro(C#)数字图像处理--2.68图像最小值滤波器 /// <summary> /// Min value filter. /// </summary> ...
- WPF应用程序嵌入第三方exe
把其它应用嵌入到C#窗口 源代码-CSDN下载 https://download.csdn.net/download/aiqinghee/10652732 WPF应用程序嵌入第三方exe - gao2 ...
- C#读取数据库内容并转换成xml文件
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\bi ...
- UWP-磁贴初识
原文:UWP-磁贴初识 简单的磁贴内容实现,来自 Bob 的视频. 为一个按钮添加点击事件,来更新磁贴. private void ChangeTileContentButton_Click(obje ...
- InfoPath分别定义New/Edit 表单
InforPath自定义表单时,默认是New/Edit是相同的,有时不能满足特殊情况,例如,某些字段在新建时不需填,编辑才改.或者编辑时不能编辑特定字段.这时最方便的方法是分别定义表单,按不同情况使用 ...
- MJPhotoBrowser 用法
一.使用方法: #import "MJPhotoBrowser.h" #import "MJPhoto.h" - (void)tapPhoto:(UIT ...
- Spring Boot配置篇(基于Spring Boot 2.0系列)
1:概述 SpringBoot支持外部化配置,配置文件格式如下所示: properties files yaml files environment variables command-line ar ...
- Mac上使用brew安装nvm来支持多版本的Nodejs
brew方式 如果机器没有安装过node,那么首先brew install nvm安装nvm. 其次需要在shell的配置文件(~/.bashrc, ~/.profile, or ~/.zshrc)中 ...