• WEB项目后端跨域请求


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.SessionState;
    
    namespace GL
    {
        public class CrossDomainHandler:IHttpModule, IRequiresSessionState
        {
            /// <summary>
            /// 释放内存
            /// </summary>
            public void Dispose()
            {
            }
    
            /// <summary>
            /// 开始请求
            /// </summary>
            /// <param name="context"></param>
            public void Init(HttpApplication context)
            {
                //页面开始请求时,绑定时间
                context.BeginRequest += new EventHandler(context_PreRequestHandlerExecute);
            }
    
            /// <summary>
            /// 请求处理
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void context_PreRequestHandlerExecute(object sender, EventArgs e)
            {
    
                HttpApplication app = (HttpApplication)sender;
                HttpContext context = app.Context;
    
                context.Response.AppendHeader("charset", "utf-8");
                context.Response.AppendHeader("defaultCharset", "utf-8");
                context.Response.AppendHeader("Content-Type", "text/html; charset=utf-8");
    
                var relativeAddr = context.Request.AppRelativeCurrentExecutionFilePath.Remove(0, 2);
                if (relativeAddr.StartsWith("Server")) 
                {
                    var url = string.Concat("http://localhost:89", relativeAddr.Substring(relativeAddr.IndexOf('/')));
                    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    var rs = request.GetRequestStream();
                    var sb = new StringBuilder("a=a&");
                    context.Request.Form.AllKeys.ToList().ForEach(name =>
                    {
                        sb.AppendFormat("{0}={1}&", name, context.Request.Form[name]);
                    });
                    var str = sb.ToString();
                    if(str.Contains('&'))
                    {
                        str = str.Substring(0, str.Length - 1);
                    }
                    var sw = new StreamWriter(rs, Encoding.UTF8);
                    sw.Write(sb.ToString());
                    sw.Close();
    
                    request.Timeout = 60 * 1000;
                    var response = request.GetResponse() as HttpWebResponse;
                    var ps = response.GetResponseStream();
                    var reader = new StreamReader(ps, Encoding.UTF8);
                    string html = reader.ReadToEnd();
                    ps.Close();
                    context.Response.Write(html);
                    context.Response.End();
                }
            }
        }
    }
    

      

  • 相关阅读:
    一个从excel导入数据错位问题
    Chrome F12 温故而知新 :因为重定向导致清空Network信息
    export / import 温故而知新
    Chrome Debugger 温故而知新:上下文环境
    微信导出表情包教程
    Oracle中判断字段是否为数字
    获取GridView中RowCommand的当前索引行
    Oracle SQL常用内置系统函数总结
    使用Task代替ThreadPool和Thread
    Visual Studio 使用及调试必知必会
  • 原文地址:https://www.cnblogs.com/liulun/p/3975132.html
Copyright © 2020-2023  润新知