应用场景:公司与外部公司数据对接,外部公司需申请指定IP访问。而本地ip经常变动,无法因ip变动时刻向外部公司申请绑定IP,给本地程序调试带来麻烦,故只能在指定ip服务器上搭建请求中转http请求;
/// <summary> /// 中转 绑定ip中转请求;参数Base64解密,gzip压缩返回; /// kk /// </summary> public class tc : IHttpHandler { public void ProcessRequest(HttpContext context) { Dictionary<string, string> dict = AnaylerUrlParaGet(context.Request); if (dict.Count == 0 || string.IsNullOrEmpty(dict["url"]) || string.IsNullOrEmpty(dict["postdata"])) { context.Response.ContentType = "text/plain"; context.Response.Write("no post data"); context.Response.End(); } HttpUtil _httputil = new HttpUtil(); string strurl = Base64.DecodeBase64IgnoreEmpty(dict["url"]); string strJson = Base64.DecodeBase64IgnoreEmpty(dict["postdata"]); string strcontext = _httputil.HttpPost(strurl, strJson, System.Text.Encoding.UTF8); context.Response.ContentType = "application/octet-stream"; context.Response.BinaryWrite(Common.EncryptHelper.Compress(strcontext)); context.Response.Flush(); context.Response.End(); } public bool IsReusable { get { return false; } } /// <summary> /// 获取GET参数 /// </summary> /// <param name="req"></param> /// <returns></returns> public Dictionary<string, string> AnaylerUrlParaGet(System.Web.HttpRequest req) { Dictionary<string, string> dict = new Dictionary<string, string>(); string tmp1; string tmp2; System.Collections.Specialized.NameValueCollection col = req.QueryString; for (int i = 0; i < col.Count; i++) { tmp1 = col.AllKeys[i].Trim(); tmp2 = col[i].Trim(); if (!dict.ContainsKey(tmp1)) { dict.Add(tmp1, tmp2); } } return dict; } }