Component Page /UserLocation.cs (C#)
1: using System;  
2: using System.Configuration;  
3: using System.Xml;  
4: using System.Net;  
5: using System.IO;  
6: using Microsoft.ApplicationBlocks.Data;  
7:   
8: namespace QuizNetOnline.Logic  
9: {  
10:     [Serializable]  
11:     public class UserLocation  
12:     {  
13:         private string _city;  
14:         private string _countryName;  
15:         private float _latitude;  
16:   
17:         private float _longitude;  
18:         private string _regionName;  
19:   
20:         public string IPaddress { get; set; }  
21:   
22:         public UserLocation() { }  
23:   
24:         public UserLocation(string paddress)  
25:         {  
26:             IPaddress = paddress;  
27:             if (ConfigurationManager.AppSettings["TurnOffipinfodb"] == null ||  
28:                 Convert.ToBoolean(ConfigurationManager.AppSettings["TurnOffipinfodb"]) == false)  
29:             {  
30:                 if (string.IsNullOrEmpty(paddress))  
31:                     return;  
32:   
33:                 if (ConfigurationManager.AppSettings["IsLocalHost"] == null ||  
34:                     Convert.ToBoolean(ConfigurationManager.AppSettings["IsLocalHost"]) == false)  
35:                 {  
36:                     ProcessIp(paddress);  
37:                 }  
38:                 else  
39:                 {  
40:                     SetLocationToUndefined();  
41:                 }  
42:             }  
43:             else  
44:             {  
45:                 SetLocationToUndefined();  
46:             }  
47:         }  
48:   
49:         private void SetLocationToUndefined()  
50:         {  
51:             CountryName = "South Africa";  
52:             RegionName = "Natal";  
53:             City = "Pinetown";  
54:             Latitude = (float)-29.815923;  
55:             Longitude = (float)30.818316;  
56:             IPaddress = "192.566.321.566";  
57:             FullLocationName = "South Africa,Natal,Pinetown";  
58:         }  
59:   
60:         internal void ProcessIp(string paddress)  
61:         {  
62:             string url =  
63:                 "http://api.ipinfodb.com/v2/ip_query.php?key=611011904d0a676e207a9e469620757ab2d9f4b18cd8290b02febdcd27b4dafa&ip=" +  
64:                 paddress;  
65:             var x = new XmlDocument();  
66:   
67:             WebRequest request;  
68:   
69:             try  
70:             {  
71:                 request = WebRequest.Create(url);  
72:   
73:                 WebResponse response = request.GetResponse();  
74:   
75:                 if (response != null)  
76:                 {  
77:                     // ReSharper disable AssignNullToNotNullAttribute  
78:                     var reader = new StreamReader(response.GetResponseStream());  
79:                     // ReSharper restore AssignNullToNotNullAttribute  
80:                     x.LoadXml(reader.ReadToEnd());  
81:                 }  
82:   
83:                 XmlNode xmlNode = x.SelectSingleNode("Response");  
84:                 if (xmlNode != null)  
85:                 {  
86:                     XmlNode xmlCountryName = xmlNode.SelectSingleNode("CountryName");  
87:                     XmlNode xmlRegionName = xmlNode.SelectSingleNode("RegionName");  
88:                     XmlNode xmlCity = xmlNode.SelectSingleNode("City");  
89:                     XmlNode xmlLatitude = xmlNode.SelectSingleNode("Latitude");  
90:                     XmlNode xmlLongitude = xmlNode.SelectSingleNode("Longitude");  
91:   
92:                     if (xmlCountryName != null)  
93:                     {  
94:                         CountryName = xmlCountryName.InnerText;  
95:                         FullLocationName = CountryName + ",";  
96:                     }  
97:                     if (xmlRegionName != null)  
98:                     {  
99:                         RegionName = xmlRegionName.InnerText;  
100:                         FullLocationName += RegionName + ",";  
101:                     }  
102:                     if (xmlCity != null)  
103:                     {  
104:                         City = xmlCity.InnerText;  
105:                         FullLocationName += City;  
106:                     }  
107:   
108:                     FullLocationName = TidyUpLocation(FullLocationName);  
109:   
110:                     if (xmlLatitude != null)  
111:                         if (xmlLongitude != null)  
112:                             if (!string.IsNullOrEmpty(xmlLatitude.InnerText) &&  
113:                                 !string.IsNullOrEmpty(xmlLongitude.InnerText))  
114:                             {  
115:                                 Latitude = float.Parse(xmlLatitude.InnerText);  
116:                                 Longitude = float.Parse(xmlLongitude.InnerText);  
117:                             }  
118:                 }  
119:   
120:             }  
121:             catch (Exception ex)  
122:             {  
123:                 SqlWrapper sqlwrapper = new SqlWrapper();  
124:                 CountryName = "Not known";  
125:                 RegionName = "Not known";  
126:                 City = "Not known";  
127:   
128:                 ErrorLog errorLog = new ErrorLog();  
129:                 errorLog.LogException("UserLocation", ex, sqlwrapper);  
130:             }  
131:         }  
132:   
133:         public float Latitude  
134:         {  
135:             get { return _latitude; }  
136:             set { _latitude = value; }  
137:         }  
138:   
139:         public float Longitude  
140:         {  
141:             get { return _longitude; }  
142:             set { _longitude = value; }  
143:         }  
144:   
145:         public string CountryName  
146:         {  
147:             get { return _countryName; }  
148:             set { _countryName = value; }  
149:         }  
150:   
151: There is a limit of 150 source code lines in the evaluation version
152: There is a limit of 150 source code lines in the evaluation version
153: There is a limit of 150 source code lines in the evaluation version
154: There is a limit of 150 source code lines in the evaluation version
155: There is a limit of 150 source code lines in the evaluation version
156: There is a limit of 150 source code lines in the evaluation version
157: There is a limit of 150 source code lines in the evaluation version
158: There is a limit of 150 source code lines in the evaluation version
159: There is a limit of 150 source code lines in the evaluation version
160: There is a limit of 150 source code lines in the evaluation version
161: There is a limit of 150 source code lines in the evaluation version
162: There is a limit of 150 source code lines in the evaluation version
163: There is a limit of 150 source code lines in the evaluation version
164: There is a limit of 150 source code lines in the evaluation version
165: There is a limit of 150 source code lines in the evaluation version
166: There is a limit of 150 source code lines in the evaluation version
167: There is a limit of 150 source code lines in the evaluation version
168: There is a limit of 150 source code lines in the evaluation version
169: There is a limit of 150 source code lines in the evaluation version
170: There is a limit of 150 source code lines in the evaluation version
171: There is a limit of 150 source code lines in the evaluation version
172: There is a limit of 150 source code lines in the evaluation version
173: There is a limit of 150 source code lines in the evaluation version
174: There is a limit of 150 source code lines in the evaluation version
175: There is a limit of 150 source code lines in the evaluation version
176: There is a limit of 150 source code lines in the evaluation version
177: There is a limit of 150 source code lines in the evaluation version
178: There is a limit of 150 source code lines in the evaluation version
179: There is a limit of 150 source code lines in the evaluation version
180: There is a limit of 150 source code lines in the evaluation version
181: There is a limit of 150 source code lines in the evaluation version
182: There is a limit of 150 source code lines in the evaluation version
183: There is a limit of 150 source code lines in the evaluation version

.NET Documentation Tool is © 2002 - 2009 Winnersh Triangle Web Solutions Limited.