using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;
namespace GEOTARGETING{
///
/// Class for geotargeting using ipgeobase.ru XML-service
/// v0.1 by Kiss_Lee_Zin (http://www.last.fm/music/insanity13)
///
public class GEOIP
{
#region "Properties"
private static bool _UseUTF8 = false;
public static bool UseUTF8 { get; set;}
///
/// Use Moscow info on request error
///
private static bool _UseDefaultCity = false;
public static bool UseDefaultCity { get; set; }
///
/// Use proxy Authentification
///
private static bool _UseAuthentification;
public static bool UseAuthentification { get; set; }
private static string _ProxyName;
public static string ProxyName { get; set; }
private static int _ProxyPort;
public static int ProxyPort { get; set; }
private static string _CredentialsName;
public static string CredentialsName { get; set; }
private static string _CredentialsPass;
public static string CredentialsPass { get; set; }
///
/// ipgeobase XML Service address
///
private static string _ServiceAddress;
public static string ServiceAddress
{
get {
if (string.IsNullOrEmpty(_ServiceAddress)) _ServiceAddress = "http://194.85.91.253:8090/geo/geo.html";
return _ServiceAddress;
}
set { _ServiceAddress = value; }
}
#endregion
public struct IPInfo
{
public string city;
public string region;
public string district;
public string lat;
public string lng;
public string inetNum;
public string inetDescr;
public string inetStatus;
public string ip;
}
///
/// Create IPInfo query for single IP
///
/// IP-Address
/// XML query
public static string createRequest(string ip)
{
return createRequest(new String[1] { ip });
}
///
/// Create IPInfo query for IP list
///
/// IP-Address
/// XML query
public static string createRequest(string[] ipList)
{
StringBuilder request = new StringBuilder();
//Create XML query
request.Append("");
foreach(string ip in ipList){
//IP Validation.
IPAddress.Parse(ip);
request.AppendFormat("{0}", ip);
}
request.Append("");
return request.ToString();
}
public static Nullable getSingleIPInfo(string ip)
{
XmlDocument xDocument = new XmlDocument();
XmlNode Node;
IPInfo IPInfo;
xDocument.InnerXml = getXMLResponse(createRequest(ip));
Node = xDocument.SelectSingleNode(string.Format("/ip-answer/ip[@value='{0}']",ip));
if (Node == null) return null;
IPInfo.ip = Node.Attributes["value"].Value;
IPInfo.city = Node.SelectSingleNode("city").InnerText;
IPInfo.region = Node.SelectSingleNode("region").InnerText;
IPInfo.district = Node.SelectSingleNode("district").InnerText;
IPInfo.lat = Node.SelectSingleNode("lat").InnerText;
IPInfo.lng = Node.SelectSingleNode("lng").InnerText;
IPInfo.inetNum = Node.SelectSingleNode("inetnum").InnerText;
IPInfo.inetDescr = Node.SelectSingleNode("inet-descr").InnerText;
IPInfo.inetStatus = Node.SelectSingleNode("inet-status").InnerText;
return IPInfo;
}
public static Dictionary getIPInfo(string[] ipList)
{
string Request = createRequest(ipList);
XmlDocument xDocument = new XmlDocument();
xDocument.InnerXml = getXMLResponse(Request);
return getIPInfo(xDocument);
}
public static Dictionary getIPInfo(XmlDocument xDocument)
{
IPInfo IPInfo;
Dictionary IPInfoList = new Dictionary();
foreach (XmlNode Node in xDocument.SelectNodes("/ip-answer/ip"))
{
IPInfo.ip = Node.Attributes["value"].Value;
IPInfo.city = Node.SelectSingleNode("city").InnerText;
IPInfo.region = Node.SelectSingleNode("region").InnerText;
IPInfo.district = Node.SelectSingleNode("district").InnerText;
IPInfo.lat = Node.SelectSingleNode("lat").InnerText;
IPInfo.lng = Node.SelectSingleNode("lng").InnerText;
IPInfo.inetNum = Node.SelectSingleNode("inetnum").InnerText;
IPInfo.inetDescr = Node.SelectSingleNode("inet-descr").InnerText;
IPInfo.inetStatus = Node.SelectSingleNode("inet-status").InnerText;
IPInfoList.Add(IPInfo.ip, IPInfo);
}
return IPInfoList;
}
public static XmlDocument getXMLResponse(XmlDocument Request)
{
XmlDocument xDocument = new XmlDocument();
xDocument.InnerXml = getXMLResponse(Request.InnerXml);
return xDocument;
}
///
/// Make a request
///
/// XMLtype IPInfo request
///
public static String getXMLResponse(string Request) {
//Create obj for request
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(ServiceAddress);
//Set query type
objRequest.Method = "POST";
objRequest.ContentType = "application/xml";
if (UseAuthentification){
objRequest.PreAuthenticate = true;
objRequest.Proxy = new WebProxy(ProxyName, ProxyPort);
objRequest.Proxy.Credentials = new NetworkCredential(CredentialsName, CredentialsPass);
}
//Open stream
using (Stream str = objRequest.GetRequestStream())
{
//send request
using (XmlWriter writer = XmlWriter.Create(str))
{
writer.WriteStartDocument();
writer.WriteStartElement("request");
writer.WriteRaw(Request);
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
string allReq = "";
StreamReader reader;
try
{
//Getting response
HttpWebResponse myResponse = (HttpWebResponse)objRequest.GetResponse();
if (UseUTF8) reader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
else reader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("Windows-1251"));
allReq = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex){
if (UseDefaultCity)
allReq = "\nМоскваМоскваЦентральный55.75578737.617634\n";
else
throw ex;
/*Do something*/
}
return allReq;
}
}
}