views:

35

answers:

1

I am making a page that accepts post data from any number of pages that I cannot change, access, or in any way control.

I need, in one way or another, to get the timezone of the user. I know, ideally the posting page would do this, but I cannot access these pages.

I've read other answers on this site and come up with 2 almost, but not quite there solutions.

First, there is javascript. I can get the javascript function to return (or change a label to) the correct value, but the problem is I need this info before the postback. I've been trying to write the timezone name on another page and read that page, but I have no idea how to begin to do that? Any other workaround to use the javascript is welcome, or any way to force call this before Page_Load is called?

    function getTimeZone()
    {
       var d = new Date()
       var gmtHours = -d.getTimezoneOffset()/60;
       var label = document.getElementById("<%=TZ.ClientID%>");
       label.textContent = "GMT " + gmtHours;
    }

The second solution is to read it from another page, and I am using this: http://ipinfodb.com/ip_query.php?ip=192.36.167.120&amp;timezone=true

(Completely random ip in there, btw)

So here is my function to get the info from that site:

        public string GetTimezone(string ip)
    {
        string address = string.Format("http://ipinfodb.com/ip_query.php?ip={0}&amp;timezone=true", ip);
        string timezone = "";

        try
        {
            XmlTextReader reader = new XmlTextReader(address);
            HttpWebRequest wrq = (HttpWebRequest)WebRequest.Create(address);
            wrq.Proxy.Credentials = CredentialCache.DefaultCredentials;
            reader = new XmlTextReader(wrq.GetResponse().GetResponseStream());

            string lastRead = "";
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    lastRead = reader.Name;
                }
                if (reader.NodeType == XmlNodeType.Text)
                {
                    if (string.Compare(lastRead, "TimezoneName", true) == 0)
                    {
                        timezone = reader.Value;
                        break;
                    }
                }
            }
        }
        catch
        {
            timezone = "";
        }

        return timezone;
    }

Basically, this works in debug mode, but when it's live only an empty string is returned. I am baffled? Is there any better way to read data from a page? I am using Request.ServerVariables["REMOTE_ADDR"] to get the ip, and that seems to be correct, since it inserts the correct ip into the database I'm using.

Here is the call:

GetTimezone(Request.ServerVariables["REMOTE_ADDR"]);

Thanks in advance for answers and advice. :)

A: 

You're getting an exception, probably because of a trust issue / firewall on the production server.

Get rid of the evil catch block so you can find out what the exception is.

SLaks
The exception I get is: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Brandi
As I suspected, your webhost is not letting your server make HTTP requests. Complain to their support department or switch to a better host.
SLaks
I changed the web config <trust level="Full" /> and that did the trick. Thanks for pointing me in the correct direction. :)
Brandi