这两天一直再看微信开发,临时在我的电脑搭了个IIS服务器做微信开发,外网也能访问了,关键是,调试太麻烦了!!
我写完代码,要将代码发布到IIS才能接收微信消息,可是在这个过程中,我不知道微信发过来的是什么,出现Bug调试麻烦,
我得 找到是哪里 出现Bug了,修改代码再发布!
有没有办法让我能够像平时那样,设个断点就行了?
于是我就写了一个简易的Http请求转发器
原理是这样:微信先请求我的IIS服务器,IIS服务器通过下面这个module 将请求 转发 到 Vs 的 IIS ExPRess,IIS Express
再返回响应内容到IIs,IIs最后将消息发到微信
看代码:
publicclassTransformer:IHttpModule
{publicvoidDispose()
{
}publicvoidInit(Httpapplicationcontext)
{
context.BeginRequest+=context_BeginRequest;
}///<summary>///要将Http请求转发 到 的 目标Url///</summary>publicUri ToUrl
{get{//从配置中读取stringtoUrl = System.Configuration.ConfigurationManager.AppSettings["TransToURL"];//判断Url是否/结尾if(!toUrl.EndsWith("/"))
{
toUrl= toUrl +"/";
}
Uri uri=newUri(toUrl);returnuri;
}
}///<summary>///目标UrlHost///</summary>publicstringToUrlHost
{get{returnToUrl.Host;
}
}///<summary>///目标Url 的端口///</summary>publicstringToPort
{get{varresult = Regex.Match(ToUrl.ToString(),@"^http://.+:(\d+)", RegexOptions.IgnoreCase);if(result.Groups.Count >1)
{returnresult.Groups[1].Value;
}else{return"";
}
}
}///<summary>///客户端直接请求的Url ,也就是 本 应用程序的 Url ,所有对该Url的请求都会被转发到 目标Url///</summary>publicUri FromUrl {get;set; }///<summary>///本应用程序Url Host///</summary>publicstringFromUrlHost
{get{returnFromUrl.Host;
}
}///<summary>///本应用程序Url 端口///</summary>publicstringFromPort
{get{varresult = Regex.Match(FromUrl.ToString(),@"^http://.+:(\d+)", RegexOptions.IgnoreCase);if(result.Groups.Count >1)
{returnresult.Groups[1].Value;
}else{return"";
}
}
}voidcontext_BeginRequest(objectsender, EventArgs e)
{stringtoUrl =this.ToUrl.ToString();
HttpApplication app= senderasHttpApplication;varrespone =app.Response;varrequest =app.Request;//初始化 本应用程序 UrlFromUrl=newUri(request.Url.ToString());//获取转换目标后的Url//将请求报文中的 Url 替换为 目标 UrlstringtempUrl =this.ReplaceHostAndPort(FromUrl.ToString(),TransType.TransTo);//创建 Http 请求 用于将 替换后 请求报文 发往 目标 UrlHttpWebRequest hRequest =HttpWebRequest.CreateHttp(tempUrl);//设置请求头this.SetRequestHead(hRequest, request);#region设置特殊请求头if(!string.IsNullOrEmpty(request.Headers["Accept"]))
{
hRequest.Accept= request.Headers["Accept"];
}if(!string.IsNullOrEmpty(request.Headers["Connection"]))
{stringconnection = request.Headers["Connection"];
hRequest.KeepAlive=string.Compare(connection,"keep-alive", StringComparison.CurrentCultureIgnoreCase) ==0;
}if(!string.IsNullOrEmpty(request.Headers["Content-Type"]))
{
hRequest.ContentType= request.Headers["Content-Type"];
}if(!string.IsNullOrEmpty(request.Headers["Expect"]))
{
hRequest.Expect= request.Headers["Expect"];
}if(!string.IsNullOrEmpty(request.Headers["Date"]))
{
hRequest.Date= Convert.ToDateTime(request.Headers["Date"]);
}if(!string.IsNullOrEmpty(request.Headers["Host"]))
{
hRequest.Host=this.ToUrlHost;
}if(!string.IsNullOrEmpty(request.Headers["If-Modified-Since"]))
{
hRequest.IfModifiedSince=Convert.ToDateTime( request.Headers["If-Modified-Since"]);
}if(!string.IsNullOrEmpty(request.Headers["Referer"]))
{
hRequest.Referer=this.ReplaceHostAndPort(request.Headers["Referer"],TransType.TransTo);
}if(!string.IsNullOrEmpty(request.Headers["User-Agent"]))
{
hRequest.UserAgent= request.Headers["User-Agent"];
}if(!string.IsNullOrEmpty(request.Headers["Content-Length"]))
{
hRequest.ContentLength=Convert.ToInt32( request.Headers["Content-Length"]);
}#endregion//判断是否是Get请求,如果不是Get就写入请求报文体if(String.Compare(request.HttpMethod,"get", StringComparison.CurrentCultureIgnoreCase) !=0)
{//设置请求体this.SetRequestBody(hRequest, request);
}//获取响应报文WebResponse hRespone=null;try{
hRespone=hRequest.GetResponse();
}catch(Exception exp)
{
respone.Write(exp.Message);
respone.End();
}//设置响应头this.SetResponeHead(hRespone, respone);#region设置特殊响应头if(!string.IsNullOrEmpty(hRespone.Headers["Content-Type"]))
{
respone.ContentType= hRespone.Headers["Content-Type"];
}if(!string.IsNullOrEmpty(hRespone.Headers["Host"]))
{
respone.AddHeader("Host", FromUrlHost);
}if(!string.IsNullOrEmpty(hRespone.Headers["Referer"]))
{
respone.AddHeader("Referer",this.ReplaceHostAndPort(hRespone.Headers["Referer"], TransType.TransBack));
}#endregion//写入响应内容this.SetResponeBody(hRespone,respone);
respone.End();
}///<summary>///设置请求头///</summary>///<param name="nrq"></param>///<param name="orq"></param>privatevoidSetRequestHead(WebRequest nrq, HttpRequest orq)
{foreach(varkeyinorq.Headers.AllKeys)
{try{
nrq.Headers.Add(key, orq.Headers[key]);
}catch(Exception)
{continue;
}
}
}///<summary>///设置请求 报文体///</summary>///<param name="nrq"></param>///<param name="orq"></param>privatevoidSetRequestBody(WebRequest nrq, HttpRequest orq)
{
nrq.Method="POST";varnStream =nrq.GetRequestStream();byte[] buffer =newbyte[1024*2];intrLength =0;do{
rLength= orq.InputStream.Read(buffer,0, buffer.Length);
nStream.Write(buffer,0, rLength);
}while(rLength >0);
}///<summary>///设置响应头///</summary>///<param name="nrp"></param>///<param name="orp"></param>privatevoidSetResponeHead(WebResponse nrp, HttpResponse orp)
{foreach(varkeyinnrp.Headers.AllKeys)
{try{
orp.Headers.Add(key, nrp.Headers[key]);
}catch(Exception)
{continue;
}
}
}///<summary>///设置响应报文体///</summary>///<param name="nrp"></param>///<param name="orp"></param>privatevoidSetResponeBody(WebResponse nrp, HttpResponse orp)
{varnStream =nrp.GetResponseStream();byte[] buffer =newbyte[1024*2];intrLength =0;do{
rLength= nStream.Read(buffer,0, buffer.Length);
orp.OutputStream.Write(buffer,0, rLength);
}while(rLength >0);
}///<summary>///替换 Host和Port///</summary>///<param name="url"></param>///<param name="type"></param>///<returns></returns>privatestringReplaceHostAndPort(stringurl, TransType type)
{stringtempToPortStr =string.IsNullOrEmpty(ToPort) ?"":":"+ToPort;stringtempFromPortStr =string.IsNullOrEmpty(FromPort) ?"":":"+FromPort;if(type==TransType.TransBack)
{returnurl.Replace(ToUrlHost + tempToPortStr, FromUrlHost +tempFromPortStr);
}else{returnurl.Replace(FromUrlHost + tempFromPortStr, ToUrlHost +tempToPortStr);
}
}
}publicenumTransType
{
TransTo,
TransBack
}