Home >Backend Development >PHP Tutorial >WEB project backend cross-domain request_PHP tutorial
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();
}
}
}
}