程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> WEB項目後端跨域請求

WEB項目後端跨域請求

編輯:關於PHP編程

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

      }

      }

      }

      }

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved