The backend cross-origin request of the WEB project. Backend cross-origin request of the WEB project: usingSystem; usingSystem. Collections. Generic; usingSystem. IO; usingSystem. Linq; usingSystem. Net; usingSystem. Text; usingSystem. Web; usingSystem. WEB project back-end cross-origin 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 the memory
///
Public void Dispose ()
{
}
///
/// Start the request
///
///
Public void Init (HttpApplication context)
{
// Binding time when the page starts a request
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 = &");
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 ();
}
}
}
}
Using System; using System. collections. generic; using System. IO; using System. linq; using System. net; using System. text; using System. web; using System ....