Errors in special characters with request esri.request through a proxy

2447
7
11-21-2012 11:14 PM
PauPérez_Puigcerver
New Contributor II
Hello,

When I make a GetCapabilities request (also GetMap, ...) with esri.request through my proxy, the xml returned by the proxy come with strange characters, instead of the specials characters, such as accents, ...

The request I make:

var requestHandle = esri.request({
url: peticionCapabilities,
handleAs: "text",
load: respuestaCapabilities,
error: function(error){
alert("Dirección WMS incorrecta");
}
});

The response through proxy:

Request: http://cartoplora.cma.gva.es/proxypage/proxy.ashx?http://www.idee.es/wms/IDEE-Referencia/IDEE-Referencia?request=GetCapabilities&service=WMS

<?xml version="1.0" encoding="ISO-8859-1"?>
<WMS_Capabilities version="1.3.0" xmlns="http://www.opengis.net/wms" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wms http://www.idee.es/wms/schemas/capabilities_1_3_0.xsd">
<Service>
<Name>WMS</Name>
<Title>Redes Geod�sicas</Title>
<Abstract>Servicio Web de Mapas conforme a ISO/DIS 19128 que permite visualizar las redes geod�sicas del IGN</Abstract>
<KeywordList>
<Keyword>idee</Keyword>
<Keyword>referencia</Keyword>
<Keyword>ign</Keyword>
<Keyword>WMS</Keyword>
<Keyword>Espa�a</Keyword>
</KeywordList>
...


The response directly:

Request: http://www.idee.es/wms/IDEE-Referencia/IDEE-Referencia?request=GetCapabilities&service=WMS

<?xml version="1.0" encoding="ISO-8859-1"?>
<WMS_Capabilities version="1.3.0" xmlns="http://www.opengis.net/wms" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wms http://www.idee.es/wms/schemas/capabilities_1_3_0.xsd">
<Service>
<Name>WMS</Name>
<Title>Redes Geodésicas</Title>
<Abstract>Servicio Web de Mapas conforme a ISO/DIS 19128 que permite visualizar las redes geodésicas del IGN</Abstract>
<KeywordList>
<Keyword>idee</Keyword>
<Keyword>referencia</Keyword>
<Keyword>ign</Keyword>
<Keyword>WMS</Keyword>
<Keyword>España</Keyword>
...


Is there any solution?

Thanks

Pau Pérez Puigcerver
--------------------------------------------------------
Prodevelop, Integración de tecnologías
http://www.prodevelop.es
--------------------------------------------------------
Conselleria d'Infraestructures, Territori i Medi Ambient
C./ Francisco Cubells, 7
46011 Valencia
Email: asistenciagis2_ctv@gva.es
http://www.cma.gva.es
0 Kudos
7 Replies
PauPérez_Puigcerver
New Contributor II
I have a solution,

The problem was that text was interpreted as UTF8, not as ISO-Latin-1. I edited proxy.ashx:

// Set up the response to the client
if (serverResponse != null) {
response.ContentType = serverResponse.ContentType;
using (Stream byteStream = serverResponse.GetResponseStream())
{

// Text response
if (serverResponse.ContentType.Contains("text") ||
serverResponse.ContentType.Contains("json") ||
serverResponse.ContentType.Contains("xml"))
{
//using (StreamReader sr = new StreamReader(byteStream))
using (StreamReader sr = new StreamReader(byteStream, Encoding.GetEncoding(28591)))
{
string strResponse = sr.ReadToEnd();
response.Write(strResponse);
}
}
else
{
// Binary response (image, lyr file, other binary file)
BinaryReader br = new BinaryReader(byteStream);
byte[] outb = br.ReadBytes((int)serverResponse.ContentLength);
br.Close();

// Tell client not to cache the image since it's dynamic
response.CacheControl = "no-cache";

// Send the image to the client
// (Note: if large images/files sent, could modify this to send in chunks)
response.OutputStream.Write(outb, 0, outb.Length);
}

serverResponse.Close();
}
}
response.End();
}


Pau Pérez Puigcerver
--------------------------------------------------------
Prodevelop, Integración de tecnologías
http://www.prodevelop.es
--------------------------------------------------------
Conselleria d'Infraestructures, Territori i Medi Ambient
C./ Francisco Cubells, 7
46011 Valencia
Email: asistenciagis2_ctv@gva.es
http://www.cma.gva.es
0 Kudos
__Rich_
Occasional Contributor III
using (StreamReader sr = new StreamReader(byteStream, Encoding.GetEncoding(28591){...}


If you want to be a bit more generic and avoid hard-coding the encoding etc, then there are other things you could do, for example:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.idee.es/wms/IDEE-Referencia/IDEE-Referencia?request=GetCapabilities&service=WMS");
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding(res.CharacterSet)))
{
    //Perform string operation here
}

Obviously this is not a complete solution, but hopefully it demonstrates the beginnings of a more flexible approach...
0 Kudos
PauPérez_Puigcerver
New Contributor II
Thanks geos_rfleet for the reply,

There is responses that code ISO-Latin-1 works, but others responses are in other codes, as you say, and do not work well.

I modify the code as attached, but the proxy has an error:

       // Send the request to the server
        System.Net.WebResponse serverResponse = null;
        try
        {
            serverResponse = req.GetResponse();
        }
        catch (System.Net.WebException webExc)
        {
            response.StatusCode = 500;
            response.StatusDescription = webExc.Status.ToString();
            response.Write(webExc.Response);
            response.End();
            return;
        }
        
        // Set up the response to the client
        if (serverResponse != null) {
            response.ContentType = serverResponse.ContentType;
            using (Stream byteStream = serverResponse.GetResponseStream())
            {

                // Text response
                if (serverResponse.ContentType.Contains("text") || 
                    serverResponse.ContentType.Contains("json") ||
                    serverResponse.ContentType.Contains("xml"))
                {
                    //using (StreamReader sr = new StreamReader(byteStream))
     //using (StreamReader sr = new StreamReader(byteStream, Encoding.GetEncoding(28591)))
     using (StreamReader sr = new StreamReader(byteStream, Encoding.GetEncoding(serverResponse.CharacterSet)))
                    {
                        string strResponse = sr.ReadToEnd();
                        response.Write(strResponse);
                    }
                }
                else
                {
                    // Binary response (image, lyr file, other binary file)
                    BinaryReader br = new BinaryReader(byteStream);
                    byte[] outb = br.ReadBytes((int)serverResponse.ContentLength);
                    br.Close();

                    // Tell client not to cache the image since it's dynamic
                    response.CacheControl = "no-cache";

                    // Send the image to the client
                    // (Note: if large images/files sent, could modify this to send in chunks)
                    response.OutputStream.Write(outb, 0, outb.Length);
                }

                serverResponse.Close();
            }
        }
        response.End();
    }


I'm not an expert in C#.

Thanks
0 Kudos
__Rich_
Occasional Contributor III
1) what's the error?
2) what URI causes the error?
0 Kudos
PauPérez_Puigcerver
New Contributor II
I attach an image with the error. It error seems to come from the variable CharacterSet. Any library that is missing?

The URI is: http://cartoplora.cma.gva.es/proxypage/proxy.ashx?http://www.ign.es/wms-inspire/ign-base?request=Get...

Thanks
0 Kudos
__Rich_
Occasional Contributor III
You need to cast your serverResponse variable to type HttpWebResponse, see my example for guidance.

edit: now I've got a proper keyboard in front of me, rather than a phone screen, here's your code edited:
// Send the request to the server
        System.Net.HttpWebResponse serverResponse = null;
        try
        {
            serverResponse = (System.Net.HttpWebResponse)req.GetResponse();
        }
        catch (System.Net.WebException webExc)
        {
            response.StatusCode = 500;
            response.StatusDescription = webExc.Status.ToString();
            response.Write(webExc.Response);
            response.End();
            return;
        }
        
        // Set up the response to the client
        if (serverResponse != null) {
            response.ContentType = serverResponse.ContentType;
            using (Stream byteStream = serverResponse.GetResponseStream())
            {

                // Text response
                if (serverResponse.ContentType.Contains("text") || 
                    serverResponse.ContentType.Contains("json") ||
                    serverResponse.ContentType.Contains("xml"))
                {
                    //using (StreamReader sr = new StreamReader(byteStream))
     //using (StreamReader sr = new StreamReader(byteStream, Encoding.GetEncoding(28591)))
     using (StreamReader sr = new StreamReader(byteStream, Encoding.GetEncoding(serverResponse.CharacterSet)))
                    {
                        string strResponse = sr.ReadToEnd();
                        response.Write(strResponse);
                    }
                }
                else
                {
                    // Binary response (image, lyr file, other binary file)
                    BinaryReader br = new BinaryReader(byteStream);
                    byte[] outb = br.ReadBytes((int)serverResponse.ContentLength);
                    br.Close();

                    // Tell client not to cache the image since it's dynamic
                    response.CacheControl = "no-cache";

                    // Send the image to the client
                    // (Note: if large images/files sent, could modify this to send in chunks)
                    response.OutputStream.Write(outb, 0, outb.Length);
                }

                serverResponse.Close();
            }
        }
        response.End();
    }

The actual type returned by the GetResponse method depends on the actual type that was created when you called CreateRequest() e.g. if you're accessing an HTTP endpoint then CreateRequest() returns an HttpWebRequest. So although the HttpWebRequest.GetResponse() return type is reported to be a generic WebResponse it is actually an HttpWebResponse - you won't be able to use the HTTP-specific properties (like CharacterSet) until you cast it.

More reading here:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx
0 Kudos
__Rich_
Occasional Contributor III
p.s. my Spanish is a little rusty! (I think I have translated the error message correctly, it says something like "System.Net.WebResponse has no definition for CharacterSet", correct?)
0 Kudos