WEB项目后端跨域请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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();
            }
        }
    }
}

  

posted @   liulun  阅读(2234)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示