[OpenLayers-Users] Revised: C# Proxy for OpenLayers

carls carlshe at 163.com
Wed Feb 11 12:08:55 EST 2009


<%@ WebHandler Language="C#" Class="Proxy" %>

using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using System.Configuration;


public class Proxy : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        
        HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(context.Request["url"]);
        request.UserAgent = context.Request.UserAgent;
        request.ContentType = context.Request.ContentType;
        request.Method = context.Request.HttpMethod;

        byte[] trans = new byte[1024];
        int offset = 0;
        int offcnt = 0;
        
        if (request.Method.ToUpper() == "POST")
        {
            Stream nstream = request.GetRequestStream();
            while (offset < context.Request.ContentLength)
            {
                offcnt = context.Request.InputStream.Read(trans, offset,
1024);
                if (offcnt > 0)
                {
                    nstream.Write(trans, 0, offcnt);
                    offset += offcnt;
                }
            }
            nstream.Close();
        }
               
        using (HttpWebResponse response =
(HttpWebResponse)request.GetResponse())
        {
            context.Response.ContentType = response.ContentType;
            
            using (Stream receiveStream = response.GetResponseStream())
            {
                offset = 0;
                offcnt = receiveStream.Read(trans, offset, 1024);
                while (offcnt>0)
                {
                    context.Response.OutputStream.Write(trans, 0, offcnt);
                    offset += offcnt;
                    if (offcnt >= 0)
                    {
                        try
                        {
                            offcnt = receiveStream.Read(trans, offset,
1024);
                        }
                        catch (Exception)
                        {
                            break;
                        } 
                    }
                    else
                        break;        
                }
            }
            context.Response.OutputStream.Close();
            context.Response.Flush(); 
            response.Close();
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

-----
Regards, Carl SHE
-- 
View this message in context: http://n2.nabble.com/upload-C--version-of-Proxy-for-OpenLayers%3A-Proxy.ashx-url%3D-tp2307110p2309924.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.




More information about the Users mailing list