WEB项目后端跨域请求_PHP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:php简单防刷计数器-PHP实例
[导读] php简单防刷计数器,lt;?phprequire_once'config.php';functioncounter{nbsp;session_start;nbsp; $sql=selectcounterfromcounter;nbsp;$result=@MySQL_query$sql;nbsp;if!empty$resultnbsp;{nbsp;$row=mysql_fetch_array$result;nbsp;$counter=++$row[co... ?ph

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

  }

  }

  }

  }

分享:PHP下载google相册到本地-PHP实例
PHP下载google相册到本地,调用方式xxx.php?user=xxx保证程序放的当前目录可写文件夹可写文件默认存放方式为./用户名/相册1描述/图片描述-1.图片后缀lt;?phpfunctionopen$host,$file{$return=;$header='';... 调用方式 xxx.php?user=xxx 保证程序放的当前目录可写文件夹

来源:模板无忧//所属分类:PHP教程/更新时间:2015-01-30
相关PHP教程