make redirection of server

Oct 9, 2011 at 2:01 AM
Edited Oct 11, 2011 at 4:34 PM

Hi l have a server make with mapserver on a intranet and i want to expose a part of this server, from a webserver a have make a sort of sharpmap wms server. With a .ashx page a make a  request  on a extranet server and from the ashx page a take the param and make the request on the intranet server. Now  i need the use the response of the intranet server as the response of the externe server. The used code will be more easy to enderstand and this is :

 public void ProcessRequest(HttpContext context)
    {
        wmsClient = new Client("http://192.168.10.20/wms/carte?", null, null);

        string param = context.Request.RawUrl.Split('?')[1];

        Client.WmsOnlineResource resource = new Client.WmsOnlineResource();
           
        Uri myUri = new Uri("http://192.168.10.20/wms/carte?" + param);
        WebRequest myWebRequest = WebRequest.Create(myUri);
     
        myWebRequest.Timeout = 5000;//_TimeOut;
     
            myWebRequest.Credentials = CredentialCache.DefaultCredentials;

       
        Image img =  null;
        try
        {
            HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
            if (myWebResponse != null)
            {
                Stream dataStream = myWebResponse.GetResponseStream();

                if (dataStream != null && myWebResponse.ContentType.StartsWith("image"))
                {
                    img = Image.FromStream(dataStream);
                  

                    dataStream.Close();
                }
                myWebResponse.Close();
            }
        }
        catch (WebException webEx)
        {
           
        }
        catch (Exception ex)
        {
           
        }



        context.Response.ContentType = "image/png";
        MemoryStream MS = new MemoryStream();
        img.Save(MS, ImageFormat.Png);
        // tidy up  
        img.Dispose();
        byte[] buffer = MS.ToArray();
        context.Response.OutputStream.Write(buffer, 0, buffer.Length);
    }

    public bool IsReusable
    {
        get { return false; }
    }

    

    private Map InitializeMap(string MapID, Size size)
    {
        //Set up the map. We use the method in the App_Code folder for initializing the map
        switch (MapID)
        {
                //Our simple world map was requested 
            
            case "WmsClient":
                return MapHelper.InitializeWmsMap(size);
            default:
                throw new ArgumentException("Invalid map '" + MapID + "' requested");
                ;
        }
    }

   the trouble with that code is that work only with open layer, and i think its because open layer don't make call of getcapability
i have other test with the following code and with this a receive error  like enable the download getcapabilty and that still just work with open layer
  public void ProcessRequest(HttpContext context)
        {
            HttpContext c = HttpContext.Current; 

            Stream dataStream = null;          
            string param = context.Request.RawUrl.Split('?')[1];
                     
            if (param.Contains("version=1.3.0"))
                param.Replace("version=1.3.0", "version=1.1.1");

            Uri myUri = new Uri("http://192.168.10.20/carte?" + param);
            WebRequest myWebRequest = WebRequest.Create(myUri);
            //myWebRequest.Method = resource.Type;
            myWebRequest.Timeout = 5000;//_TimeOut;
            //if (_Credentials != null)
            //     myWebRequest.Credentials = _Credentials;
            // else
            myWebRequest.Credentials = CredentialCache.DefaultCredentials;

             HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
             if (myWebResponse != null)
             {

                 WebResponse res = myWebRequest.GetResponse();
                 Stream resStream = res.GetResponseStream();

                 MemoryStream ms = new MemoryStream();
                 byte[] buffer = new byte[2048];
                 int bytesRead = 0;

                 do
                 {
                     bytesRead = resStream.Read(buffer, 0, buffer.Length);
                     ms.Write(buffer, 0, bytesRead);
                 }
                 while (bytesRead != 0);

                 resStream.Close();
                 res.Close();

                 byte[] data = ms.ToArray();
                 ms.Close();

                 c.Response.ContentType = res.Headers["content-type"];
                c.Response.OutputStream.Write(data, 0, data.Length);
                //  c.Response.OutputStream.Write(bytes, 0, bytes.Length);

                 c.Response.OutputStream.Close();
             }                               

        }
someone have other suggestion
thanks
Oct 11, 2011 at 2:54 AM

i have look on some code of sharmap and i have find that :may be this could solve my trouble

if (String.Compare(context.Request.Params["REQUEST"], "GetCapabilities", ignorecase) == 0)
            {
                //Service parameter is mandatory for GetCapabilities request
                if (context.Request.Params["SERVICE"] == null)
                {
                    WmsException.ThrowWmsException("Required parameter SERVICE not specified");
                    return;
                }

                if (String.Compare(context.Request.Params["SERVICE"], "WMS") != 0)
                    WmsException.ThrowWmsException(
                        "Invalid service for GetCapabilities Request. Service parameter must be 'WMS'");

                XmlDocument capabilities = Capabilities.GetCapabilities(map, description);
                context.Response.Clear();
                context.Response.ContentType = "text/xml";
                XmlWriter writer = XmlWriter.Create(context.Response.OutputStream);
                capabilities.WriteTo(writer);
                writer.Close();
                context.Response.End();
            }

Oct 11, 2011 at 4:34 PM

if that can interrest someone i have solve my trouble with that

public void ProcessRequest(HttpContext context)
        {
            HttpContext c = HttpContext.Current; 

            Stream dataStream = null;           
            string param = context.Request.RawUrl.Split('?')[1];

            if (param.Contains("VERSION=1.3.0"))
              param = param.Replace("VERSION=1.3.0", "VERSION=1.1.1");

            if (param.Contains("version=1.3.0"))
              param = param.Replace("version=1.3.0", "version=1.1.1");

            if (param.Contains("version=\"1.3.0\""))
              param = param.Replace("version=\"1.3.0\"", "version=\"1.1.1\"");

            // create a writer and open the file
            TextWriter tw = new StreamWriter(@"C://param.txt");

            // write a line of text to the file
            tw.WriteLine(param);

            // close the stream
            tw.Close();

            Uri myUri = new Uri("http://192.168.10.20/cgi-wms/carte_routiere?" + param);
            WebRequest myWebRequest = WebRequest.Create(myUri);
            //myWebRequest.Method = resource.Type;
            myWebRequest.Timeout = 15000;//_TimeOut;
            //if (_Credentials != null)
            //     myWebRequest.Credentials = _Credentials;
            // else
            myWebRequest.Credentials = CredentialCache.DefaultCredentials;

            HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
            if (myWebResponse != null)
            {
                WebResponse res = myWebRequest.GetResponse();
                Stream resStream = res.GetResponseStream();               

                if (context.Request.Params["SERVICE"] == null)
                {
                     //WmsException.ThrowWmsException("Required parameter SERVICE not specified");
                    return;
                }

                if (String.Compare(context.Request.Params["SERVICE"], "WMS") != 0)
                {
                     //WmsException.ThrowWmsException( "Invalid service for GetCapabilities Request. Service parameter must be 'WMS'");
                    return;
                }
                                  
                if (String.Compare(context.Request.Params["REQUEST"], "GetCapabilities", true) == 0)
                {
                    MemoryStream ms = new MemoryStream();
                    byte[] buffer = new byte[8 * 1024];
                    int len;
                    while ((len = resStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, len);
                    }     
                   
                    byte[] data = ms.ToArray();
                    ms.Close();
                    c.Response.Clear();
                    c.Response.ContentType = "text/xml";
                    c.Response.OutputStream.Write(data, 0, data.Length);                   
                    c.Response.End();
                     
                 }
                 else
                 {
                    byte[] buffer = new byte[2048];
                    int bytesRead = 0;
                    MemoryStream ms = new MemoryStream();

                    do
                    {
                        bytesRead = resStream.Read(buffer, 0, buffer.Length);
                        ms.Write(buffer, 0, bytesRead);
                    }
                    while (bytesRead != 0);

                    resStream.Close();
                    res.Close();

                    byte[] data = ms.ToArray();
                    ms.Close();

                    c.Response.ContentType = res.Headers["content-type"];
                    c.Response.OutputStream.Write(data, 0, data.Length);
                     //  c.Response.OutputStream.Write(bytes, 0, bytes.Length);

                    c.Response.OutputStream.Close();
                 }
             }
        }