ProxyHandler.ashx
上传用户:huazai0421
上传日期:2008-05-30
资源大小:405k
文件大小:3k
源码类别:

SilverLight

开发平台:

C#

  1. <%@ WebHandler Language="C#" Class="ProxyHandler" %>
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. public class ProxyHandler : IHttpHandler
  7. {
  8.     public void ProcessRequest(HttpContext context)
  9.     {
  10.         string uri = context.Request.RawUrl.Substring(context.Request.RawUrl.IndexOf("?") + 1);
  11.         if (uri.StartsWith("ping"))
  12.         {
  13.             context.Response.Write("<html><body>Hello ProxyHandler</body></html>");
  14.             return;
  15.         }
  16.             
  17.         System.Net.WebRequest req = System.Net.WebRequest.Create(new Uri(uri));
  18.         req.Method = context.Request.HttpMethod;
  19.         //req.Timeout = 15000;
  20.         // Set body of request for POST requests
  21.         if (context.Request.InputStream.Length > 0)
  22.         {
  23.             byte[] bytes = new byte[context.Request.InputStream.Length];
  24.             context.Request.InputStream.Read(bytes, 0, (int)context.Request.InputStream.Length);
  25.             req.ContentLength = bytes.Length;
  26.             req.ContentType = "application/x-www-form-urlencoded";
  27.             System.IO.Stream outputStream = req.GetRequestStream();
  28.             outputStream.Write(bytes, 0, bytes.Length);
  29.             outputStream.Close();
  30.         }
  31.         System.Net.WebResponse res = req.GetResponse();
  32.         context.Response.ContentType = res.ContentType;
  33.         // Text responses
  34.         if (res.ContentType.Contains("text") || res.ContentType.Contains("application/vnd.ogc"))
  35.         {
  36.             System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream());
  37.             string strResponse = sr.ReadToEnd();
  38.             context.Response.Write(strResponse);
  39.         }
  40.         // Convert all png images to 32-bit for full alpha (transparency support) in Silverlight    
  41.         else if (res.ContentType.Contains("/png"))
  42.         {
  43.             System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(res.GetResponseStream());            
  44.             System.IO.MemoryStream ms = new System.IO.MemoryStream();
  45.             bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  46.             context.Response.BinaryWrite(ms.ToArray());
  47.             
  48.         }else{ 
  49.             
  50.             // Binary responses (non-png images)            
  51.             byte[] b;
  52.             using (System.IO.BinaryReader br = new System.IO.BinaryReader(res.GetResponseStream()))
  53.             {
  54.                 b = br.ReadBytes(500000);
  55.                 br.Close();
  56.             }
  57.             context.Response.BinaryWrite(b);
  58.         }
  59.         context.Response.End();
  60.     }
  61.     // Necessary for IHttpHandler implementation
  62.     public bool IsReusable
  63.     {
  64.         get
  65.         {
  66.             return false;
  67.         }
  68.     }
  69. }