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. ---Ubuntu 14.04 虚拟机器和主机时间同步

    先把vmware tool 装好! sudo /usr/bin/vmware-toolbox-cmd  timesync enable

  2. bash脚本编程之二 条件判断and 逻辑运算

    1.条件测试结构 1) if/then结构: 判断命令列表的退出码是否为0,0为成功. 如果if和then在条件判断的同一行上的话, 必须使用分号来结束if表达式: if和then都是关键字. 关键字 ...

  3. WordPress建站指南

    WordPress建站指南(1)   写在前面: 3月份用10天零碎时间火速完成了建站,后台95%的工作都交给了WP(WordPress).如果想偷懒的话,WP是一个绝好的选择,估计有个小半天就建完收 ...

  4. Python全栈--7.3--模块补充configparser--logging--subprocess--os.system--shutil

    模块补充: 一.configparser用于处理特定格式的文件,其本质是利用open来操作文件 继承到2版本 configparser 实现了更多智能特征,更有壳预见性,新的应用更偏好这个版本, 处理 ...

  5. win7 64位 mongodb2.6.0 安装服务启动

    Workaround to install as a service You can manually install 2.6.0 as a service on Windows from an Ad ...

  6. MyScript 手写识别数学公式、图形 自动计算

    项目的地址  http://git.oschina.net/bimingcong/MyScript #说明:MyScript是一种能够自动识别用户在屏幕上的手势,然后转化为相应的数学公式.图形(比如三 ...

  7. Python:集合

    set集合: 特点:无序,元素不重复 功能:关系测试,去重 集合创建: >>> s = set('python') >>> s {'p', 't', 'y', 'h ...

  8. 【摘】Chrome解决高版本Stable Beta扩展程序强制停用问题

    博客园插件遇到这个问题,下述摘自百度贴吧,原文地址http://tieba.baidu.com/p/3091171066 1]下载组策略模板 chrome.adm 2] Win+R gpedit.ms ...

  9. SDWebImage实现原理--两张图带你看懂

    SDWebImage底层实现有沙盒缓存机制,主要由三块组成:1.内存图片缓存,2.内存操作缓存,3.磁盘沙盒缓存 SDWebImage GitHub地址 版本4.0.0 一.SDWebImage时序图 ...

  10. 正向代理与反向代理的区别【Nginx读书笔记】(zz)

    正向代理与反向代理的区别[Nginx读书笔记]       正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单的说,我是一个用户,我访问不了某网站,但是我能访问一个代理服务器 ...