[OpenLayers-Users] C# version of Proxy for OpenLayers:Proxy.ashx?url=

Mark Lidstone Mark.Lidstone at bmtcordah.com
Wed Apr 8 11:14:23 EDT 2009


Ah!  A question I can answer!  :)
 
It's a misuse of the "offset" argument in the script.  If the page had
fit into a single buffer (1024 bytes in that case) the problem wouldn't
have shown up.
 
I've included below a version that I've just put together using my own
code and added the POST support (and a couple of the request headers)
from the code you found on the list:

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

using System;
using System.Web;

public class WfsProxy: IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        System.Net.WebRequest req =
System.Net.WebRequest.Create(context.Request["u"]);
        if (req is System.Net.HttpWebRequest)
            ((System.Net.HttpWebRequest)req).UserAgent =
context.Request.UserAgent;
        req.ContentType = context.Request.ContentType;
        req.Method = context.Request.HttpMethod;
        
        int nRead = 0;
        byte[] baBuffer = new byte[BUFF_SIZE];
        if (req.Method == "POST")//The WebRequest object takes care of
case for you
        {
            int nTmp = 0;
            while (nTmp < context.Request.ContentLength)
            {
                nRead = context.Request.InputStream.Read(baBuffer, 0,
BUFF_SIZE);
                if (nRead > 0)
                {
                    req.GetRequestStream().Write(baBuffer, 0, nRead);
                    nTmp += nRead;
                }
            }
            req.GetRequestStream().Flush();
            req.GetRequestStream().Close();
        }
        System.Net.WebResponse resp = req.GetResponse();
        context.Response.ContentType = resp.ContentType;
        System.IO.Stream strm = resp.GetResponseStream();
        nRead = strm.Read(baBuffer, 0, BUFF_SIZE);
        System.IO.Stream strmOut = context.Response.OutputStream;
        
        while (nRead != 0)
        {
            strmOut.Write(baBuffer, 0, nRead);
            nRead = strm.Read(baBuffer, 0, BUFF_SIZE);
        }

        strmOut.Flush();
        strmOut.Close();
    }

    private const int BUFF_SIZE = 8192;
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

I hope this helps,

Mark Lidstone
Senior Software Engineer
Tel: +44 (0)23 80232222; Fax: +44 (0)23 80232891

BMT Cordah Ltd
Grove House
7 Ocean Way
Ocean Village
Southampton
SO14 3TJ



 


________________________________

	From: users-bounces at openlayers.org
[mailto:users-bounces at openlayers.org] On Behalf Of Paul james
	Sent: 08 April 2009 15:20
	To: users at openlayers.org
	Subject: Re: [OpenLayers-Users] C# version of Proxy for
OpenLayers:Proxy.ashx?url=
	
	
	Hello!
	I tried that, but if I test any page like that :
	Proxy.ashx?url=http://www.google.com
	I got a empty blank page...
	
	Any idea?
	P
	
	---------------------------------
	
	<%@ 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;
	        }
	    }
	
	}
	



BMT Cordah Ltd.
A member of the BMT group of companies
Registered Office: Scotstown Road, Bridge of Don, Aberdeen, AB23 8HG, UK
Registered in Scotland No. 163413
http://www.bmtcordah.com/
http://www.bmt.org/

The contents of this e-mail and any attachments are intended only for the use of the e-mail addressee(s) shown. If you are not that person, or one of those persons, you are not allowed to take any action based upon it or to copy it, forward, distribute or disclose the contents of it and you should please delete it from your system. BMT Cordah Limited does not accept liability for any errors or omissions in the context of this e-mail or its attachments, which arise as a result of Internet transmission, nor accept liability for statements which are those of the author and not clearly made on behalf of BMT Cordah Limited.




More information about the Users mailing list