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();
}
}
}
}

  

WEB项目后端跨域请求的更多相关文章

  1. 基于.Net Framework 4.0 Web API开发(5):ASP.NET Web APIs AJAX 跨域请求解决办法(CORS实现)

    概述:  ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是在使用API的时候总会遇到跨域请求的问题,特 ...

  2. $Django 前后端之 跨域问题(同源策略) vue项目(axios跨域请求数据)

    1 跨域问题(多个域之间的数据访问) #同源策略(ip port 协议全部相同) #本站的只能请求本站域名的数据 #CORS实现(跨域资源共享) #实现CORS通信的关键是服务器.只要服务器实现了CO ...

  3. Vue-cli 创建的项目如何跨域请求

    感谢BeArchitect的技术支持 问题描述: 使用 Vue-cli 创建的项目,开发地址是 localhost:8023,需要访问 localhost:9000 上的接口 分析原因: 不同域名之间 ...

  4. Vue-cli 创建的项目配置跨域请求(通过反向代理)---配置多个代理--axios请求

    问题描述: 使用 Vue-cli 创建的项目,开发地址是 localhost:8080,需要访问 localhost:9000 或https://m.maoyan.com或http://image.b ...

  5. php 后端跨域请求

    header("Access-Control-Allow-Origin: http://a.com"); // 允许a.com发起的跨域请求 //如果需要设置允许所有域名发起的跨域 ...

  6. Maven Web项目解决跨域问题

    跨域问题目前笔者所用到的方案大致有三种:jsonp,SpringMVC 4以上注解方式和cros三方过滤器. Jsonp JSONP(JSON with Padding)是一个非官方的协议,它允许在服 ...

  7. web三种跨域请求数据方法

    以下测试代码使用php,浏览器测试使用IE9,chrome,firefox,safari <!DOCTYPE HTML> <html> <head>     < ...

  8. VUE npm run build的项目出现跨域请求的问题npm run dev没有这个问题

    报错信息 Access to XMLHttpRequest at 'http://platformapi-test.lih-elearning.cn/api/v1/login' from origin ...

  9. Spring 完美配置跨域请求

    在SpringBoot2.0 上的跨域 用以下代码配置 即可完美解决你的前后端跨域请求问题 import org.springframework.context.annotation.Bean; im ...

随机推荐

  1. easyui1.32 各种问题汇总

    问题一 场景:tab切换,每个tab里用div放一个dataGrid,默认display:none隐藏,当display:'block'的时候,dataGrid会显示不全,仅显示一条线. 解决方法:切 ...

  2. ADAS技术应用

    ADAS技术应用: LDW:Lane Departure Warning 车道偏离警告VD: Vihicle Detection 车辆检测FCW: Front Collision Warning 前向 ...

  3. Lua metatable & metamethod

    [Lua metatable & metamethod] Lua中的每个值都有一套预定义的操作集合,比如数字是可以相加的,字符串是可以连接的,但是对于两个table类型,则不能直接进行“+”操 ...

  4. [python] defaultdict

    import collections s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] # defaul ...

  5. 运行时报错-Verify the Developer App certificate for youraccount is trusted on your device. Open Settings on Mayoyi_sakura and navigate to General -> Device Management, then select your

    解决方法:打开手机设置->通用->设备管理,找到编辑工程时的ID资料,点击允许即可.

  6. elasticsearch Java API汇总

    http://blog.csdn.net/changong28/article/details/38445805#comments 3.1 集群的连接 3.1.1 作为Elasticsearch节点 ...

  7. 在Linux下安装配置phpMyAdmin步骤

    本文于CentOS 6.5下测试可行,欢迎转载和指正. phpMyAdmin是一种基于Web的免费MySQL管理工具.phpMyAdmin是用PHP编写的,因此只有支持PHP的服务器才能使用phpMy ...

  8. instr函数

    在Oracle/PLSQL中,instr函数返回要截取的字符串在源字符串中的位置. 语法如下:instr( string1, string2 [, start_position [, nth_appe ...

  9. 模板(Template)

    最近阅读google chromium base container stack_container代码,深刻感觉到基础知识不扎实. // Casts the buffer in its right ...

  10. 边表+SPFA

    传说中效率很NB的单元最短路径算法,传说中时间复杂度为O(kE),k为长度,平均值为2,不知道这话是谁说的,一说流传oi界几年了 边表就是数组模拟邻接表,没学会很难,学会很简单的样子啊 #includ ...