Home >Backend Development >PHP Tutorial >WEB project backend cross-domain request_PHP tutorial

WEB project backend cross-domain request_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:08:40871browse

WEB project backend cross-domain request

 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

 {

 ///

 /// Release memory

 ///

public void Dispose()

 {

 }

 ///

 /// Start request

 ///

 ///

public void Init(HttpApplication context)

 {

//Binding time when the page starts requesting

Context.BeginRequest += new EventHandler(context_PreRequestHandlerExecute);

 }

 ///

 /// Request processing

 ///

 ///

 ///

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

 }

 }

 }

 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/950333.htmlTechArticleWEB project backend cross-domain request using System; using System.Collections.Generic; using System.IO; using System .Linq; using System.Net; using System.Text; using System.Web; using System....
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn