WEB项目后端跨域请求
来源: 阅读:773 次 日期:2015-02-02 15:47:32
温馨提示: 小编为您整理了“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

{

///

/// 释放内存

///

public void Dispose()

{

}

///

/// 开始请求

///

///

public void Init(HttpApplication context)

{

//页面开始请求时,绑定时间

context.BeginRequest += new EventHandler(context_PreRequestHandlerExecute);

}

///

/// 请求处理

///

///

///

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("", 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();

}

}

}

}

更多信息请查看IT技术专栏

更多信息请查看网络编程
手机网站地址:WEB项目后端跨域请求
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2025国考·省考课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00)版权所有:
云南网警报警专用图标
Baidu
map