There is a free comunity based project for geo-locating IP's located here: http://www.hostip.info
Here is a small description excerpt from their home page: - - - - -
My IP Address Lookup - Geolocate Visitors by IP Address Hostip.info is a community-based project to geolocate IP addresses, making the database freely available (see below) but it needs you to put in your city to make it work. It only takes 10 seconds, and you'll get a warm fuzzy feeling of 'doing the right
thing' :-)
- - - - -
Anyway, located here: http://www.hostip.info/use.html
is the information to copy and paste some simple html code to add visitor's ip based location to a web page (even includes an animated map object in JAVA you can add with just a few lines).
What I want to know is how I can apply the following "simple" info quoted below from that page: - - - - -
Simple GET
That said, there is an easy HTTP oriented API to locate IP addresses and Geocode them. If you don't supply the "?ip=aa.bb.cc.dd" bit, then the ip address lookup of the calling machine will be located instead (here, the aa,bb,cc,dd are decimal digits). If you
add &position=true to the end of the URL then latitude and longitude will be returned also. Both HTML and XML formats are supplied for your convenience.
http://api.hostip.info/?ip=12.215.42.19
[use the URL above for an example - XML too long to paste below]
--------------------------------------------------------------------------
I experimented with a simple form (method="get", and action="http://api.hostip.info/get_html.php")
and also with a simple inline frame with the SRC set to the api.hostinfo address, and both methods
work as far as getting the IP location data from hostip into the browser.
The problem is I want to get this information in a way my .aspx programs can actually use. (I see no use in a form that opens a blank page with the hostip response in it, or in a frame - inline or otherwise - since I have no way of then reading
that info even with resorting to Javascript. ((different domains - access denied))).
So, how do I do an HTTP GET as per above from ASP.Net? (heck I'd be happy at this point even to resort to javascript to somehow get and then copy the hostip response into an input field so my program can at least see it when form is submitted).
I'm hoping "simple" http: GET's and POST's are not hugely involved from ASP.Net(?)
(Visual Studio 2003 Pro, Visual Basic, but I can usually decipher C#). It'd be nice to then contribute a (hopefully simple) Asp.Net solution to the folks over at
http://www.hostip.info to share with people as well... I just plain think what they're doing over there is really neat. :-)
ok, it sounds like what you want to do is when your page is loading
get the users IP address
make a request to this service sending the users IP
get back the physical location based on the user IP
display this physical location to the user when you page renders
if the above is true, you have a couple of options
1. load the xml data into a XmlDocument object and retrieve the values you need
2. use the HttpWebRequest object to GET data from this service and parse out the values
I personally would use xml, code would be something like this (untested)
private void Page_Load(object sender, System.EventArgs e)
{
// NOTE: this may not always have a value
string ip = Request.ServerVariables["REMOTE_ADDR"];
string url = "http://api.hostip.info/?ip=" + ip;
string location = String.Empty;
XmlDocument xml = new XmlDocument();
xml.Load(url);
XmlNode node = xml.SelectSingleNode("//Hostip/gml:name");
if(node != null)
{
location = node.InnerText; // or maybe node.Value?
}
}
if you want to use a GET technique do a google search on HttpWebRequest
First I want to thank you very much for your interest, I'm *almost* there.. :-)
Here is your almost working code converted to Visual Basic in Page_Load:
If Not Page.IsPostBack
Then
Dim City, State, Country, Longitude, Latitude, Temp
As String
Dim urlIP As
String = "http://api.hostip.info/"
'urlIP += "?=" & Request.UserHostAddress & "&position=true" '<--for use on actual website
urlIP += "?=66.82.9.81&position=true" 'Don't send useless 127.0.0.1 from home machine
Dim xml As
New System.Xml.XmlDocument
xml.Load(urlIP)
Dim node As System.Xml.XmlNode = xml.SelectSingleNode("//Hostip/gml:name")
Temp = node.Value
Say(Temp)
End Sub
Say(Temp) is a little routine that does an Alert( ) box when page loads.
Everything works up to when it tries to parse the xml.selectSingleNode line, as per the following error page:
Server Error in '/GetISP' Application.
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Xml.XPath.XPathException: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
Source Error:
Line 39: Dim xml As New System.Xml.XmlDocument
Line 40: xml.Load(urlIP)
Line 41: Dim node As System.Xml.XmlNode = xml.SelectSingleNode("//Hostip/gml:name")
Line 42: Temp = node.Value
Line 43: Say(Temp)
I found it by searching Google with "XPathException: Namespace Manager or XsltContext needed"
if that link doesn't have enough info check out
http://msdn.microsoft.com for XmlNamespaceManager object, I have never used it
Well, nodes not prefixed with gml didn't work either, so while purists may wince I finally decided to solve the problem by shamelessly running away from it and just parse the xml myself. In addition I discovered that <gml:name> comes back as (Private Address)
when fed ip's like 127.0.0.1 or 0.0.0.0 and such so I attempt guard against unknown ip's as well.
Here is the *working* code in VB, with a quick & dirty GetNode() function:
If Not Page.IsPostBack
Then
Dim City, State, Country, CountryShort, Longitude, Latitude, xmlResult, Temp
As String
Dim urlIP
As String = "http://api.hostip.info/"
'urlIP += "?=" & Request.UserHostAddress & "&position=true '<----for use on actual website
urlIP += "?=66.82.9.81&position=true" 'don't send useless 127.0.0.1 from home machine
Dim xml As
New System.Xml.XmlDocument
xml.Load(urlIP)
xmlResult = xml.InnerXml
'chop off everything up to & including the <Hostip> tag
xmlResult = Mid(xmlResult, InStr(xmlResult, "<Hostip>") + 8)
City = GetNode(xmlResult, "<gml:name>")
If City <> "(Private Address)"
Then
State = Mid(City, InStr(City, ",") + 2) 'eliminate space after comma
City = Mid(City, 1, InStr(City, ",") - 1)
Country = GetNode(xmlResult, "<countryName>")
CountryShort = GetNode(xmlResult, "<countryAbbrev>")
Longitude = GetNode(xmlResult, "<gml:coordinates>")
Latitude = Mid(Longitude, InStr(Longitude, ",") + 1)
Longitude = Mid(Longitude, 1, InStr(Longitude, ",") - 1)
End If
'combine all the now distinctly workable elements and display em
Temp = "Country: " & Country & " (" & CountryShort & ")" & vbCrLf
Temp += "Region/City: " & City & vbCrLf & "State: " & State & vbCrLf
Temp += "Longitude: " & Longitude & vbCrLf & "Latitude: " & Latitude & vbCrLf
txtHostIP.Text = Temp ' regular asp textbox
End If
- - - - - -
Function GetNode(ByRef rawXML
As String,
ByVal Target As
String) As
String
Dim X, Y
As Integer, CloseTag
As String
X = InStr(rawXML, Target) + Len(Target) ' get start position of target value into X
CloseTag = "</" & Mid(Target, 2) 'create matching closing tag
Y = InStr(rawXML, CloseTag) - X 'start of closing tag minus X = Length of value
Return Mid(rawXML, X, Y)
' return specified target value
End Function
- - - - - - - - - -
Since I hate following message threads that never result in a posted working answer I wanted to throw my semi-final "if it works don't fix it" solution in for anybody who may be following this...
:-)
JZComputer
Member
60 Points
12 Posts
How to do an HTTP GET from ASP.Net?
Jan 21, 2006 03:58 AM|LINK
There is a free comunity based project for geo-locating IP's located here:
http://www.hostip.info
Here is a small description excerpt from their home page:
- - - - -
My IP Address Lookup - Geolocate Visitors by IP Address
Hostip.info is a community-based project to geolocate IP addresses, making the database freely available (see below) but it needs you to put in your city to make it work. It only takes 10 seconds, and you'll get a warm fuzzy feeling of 'doing the right thing' :-)
- - - - -
Anyway, located here: http://www.hostip.info/use.html
is the information to copy and paste some simple html code to add visitor's ip based location to a web page (even includes an animated map object in JAVA you can add with just a few lines).
What I want to know is how I can apply the following "simple" info quoted below from that page:
- - - - -
Simple GET
That said, there is an easy HTTP oriented API to locate IP addresses and Geocode them. If you don't supply the "?ip=aa.bb.cc.dd" bit, then the ip address lookup of the calling machine will be located instead (here, the aa,bb,cc,dd are decimal digits). If you add &position=true to the end of the URL then latitude and longitude will be returned also. Both HTML and XML formats are supplied for your convenience.
http://api.hostip.info/country.php
US
http://api.hostip.info/get_html.php?ip=12.215.42.19
Country: UNITED STATES (US)
City: Sugar Grove, IL
http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true
Country: UNITED STATES (US)
City: Sugar Grove, IL
Latitude: 41.7696
Longitude: -88.4588
http://api.hostip.info/?ip=12.215.42.19
[use the URL above for an example - XML too long to paste below]
--------------------------------------------------------------------------
I experimented with a simple form (method="get", and action="http://api.hostip.info/get_html.php")
and also with a simple inline frame with the SRC set to the api.hostinfo address, and both methods work as far as getting the IP location data from hostip into the browser.
The problem is I want to get this information in a way my .aspx programs can actually use. (I see no use in a form that opens a blank page with the hostip response in it, or in a frame - inline or otherwise - since I have no way of then reading that info even with resorting to Javascript. ((different domains - access denied))).
So, how do I do an HTTP GET as per above from ASP.Net? (heck I'd be happy at this point even to resort to javascript to somehow get and then copy the hostip response into an input field so my program can at least see it when form is submitted).
I'm hoping "simple" http: GET's and POST's are not hugely involved from ASP.Net(?)
(Visual Studio 2003 Pro, Visual Basic, but I can usually decipher C#). It'd be nice to then contribute a (hopefully simple) Asp.Net solution to the folks over at http://www.hostip.info to share with people as well... I just plain think what they're doing over there is really neat. :-)
Thanks,
John
jhouse
Participant
1856 Points
374 Posts
Re: How to do an HTTP GET from ASP.Net?
Jan 21, 2006 04:14 PM|LINK
ok, it sounds like what you want to do is when your page is loading
get the users IP address
make a request to this service sending the users IP
get back the physical location based on the user IP
display this physical location to the user when you page renders
if the above is true, you have a couple of options
1. load the xml data into a XmlDocument object and retrieve the values you need
2. use the HttpWebRequest object to GET data from this service and parse out the values
I personally would use xml, code would be something like this (untested)
private void Page_Load(object sender, System.EventArgs e) { // NOTE: this may not always have a value string ip = Request.ServerVariables["REMOTE_ADDR"]; string url = "http://api.hostip.info/?ip=" + ip; string location = String.Empty; XmlDocument xml = new XmlDocument(); xml.Load(url); XmlNode node = xml.SelectSingleNode("//Hostip/gml:name"); if(node != null) { location = node.InnerText; // or maybe node.Value? } }if you want to use a GET technique do a google search on HttpWebRequest
hope that helps
JZComputer
Member
60 Points
12 Posts
Re: How to do an HTTP GET from ASP.Net?
Jan 21, 2006 09:21 PM|LINK
First I want to thank you very much for your interest, I'm *almost* there.. :-)
Here is your almost working code converted to Visual Basic in Page_Load:
If Not Page.IsPostBack Then
Dim City, State, Country, Longitude, Latitude, Temp As String
Dim urlIP As String = "http://api.hostip.info/"
'urlIP += "?=" & Request.UserHostAddress & "&position=true" '<--for use on actual website
urlIP += "?=66.82.9.81&position=true" 'Don't send useless 127.0.0.1 from home machine
Dim xml As New System.Xml.XmlDocument
xml.Load(urlIP)
Dim node As System.Xml.XmlNode = xml.SelectSingleNode("//Hostip/gml:name")
Temp = node.Value
Say(Temp)
End Sub
Say(Temp) is a little routine that does an Alert( ) box when page loads.
Everything works up to when it tries to parse the xml.selectSingleNode line, as per the following error page:
Server Error in '/GetISP' Application.
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Xml.XPath.XPathException: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
Source Error:
Source File: c:\inetpub\wwwroot\GetISP\GetISP.aspx.vb Line: 41
Stack Trace:
So, apparently I need to somehow pre-define the open source gml standard or I failed to import a namespace into the project somewhere (?).
You sure were mighty close with just offhandedly zapping out that little routine . :-) Is this due to some kind of difference with gml from xml?
Thanks!
John
jhouse
Participant
1856 Points
374 Posts
Re: How to do an HTTP GET from ASP.Net?
Jan 21, 2006 09:50 PM|LINK
first thing I would do is check that the code will work for the nodes that are not prefixed with gml
i.e. try xml.SelectSingleNode("//Hostip/countryName") to get the country name to display.
then take a look at this link - http://www.dotnet247.com/247reference/msgs/46/232788.aspx
I found it by searching Google with "XPathException: Namespace Manager or XsltContext needed"
if that link doesn't have enough info check out http://msdn.microsoft.com for XmlNamespaceManager object, I have never used it
good luck!
JZComputer
Member
60 Points
12 Posts
Re: How to do an HTTP GET from ASP.Net?
Jan 22, 2006 05:08 AM|LINK
Well, nodes not prefixed with gml didn't work either, so while purists may wince I finally decided to solve the problem by shamelessly running away from it and just parse the xml myself. In addition I discovered that <gml:name> comes back as (Private Address) when fed ip's like 127.0.0.1 or 0.0.0.0 and such so I attempt guard against unknown ip's as well.
Here is the *working* code in VB, with a quick & dirty GetNode() function:
If Not Page.IsPostBack Then
Dim City, State, Country, CountryShort, Longitude, Latitude, xmlResult, Temp As String
Dim urlIP As String = "http://api.hostip.info/"
'urlIP += "?=" & Request.UserHostAddress & "&position=true '<----for use on actual website
urlIP += "?=66.82.9.81&position=true" 'don't send useless 127.0.0.1 from home machine
Dim xml As New System.Xml.XmlDocument
xml.Load(urlIP)
xmlResult = xml.InnerXml
'chop off everything up to & including the <Hostip> tag
xmlResult = Mid(xmlResult, InStr(xmlResult, "<Hostip>") + 8)
City = GetNode(xmlResult, "<gml:name>")
If City <> "(Private Address)" Then
State = Mid(City, InStr(City, ",") + 2) 'eliminate space after comma
City = Mid(City, 1, InStr(City, ",") - 1)
Country = GetNode(xmlResult, "<countryName>")
CountryShort = GetNode(xmlResult, "<countryAbbrev>")
Longitude = GetNode(xmlResult, "<gml:coordinates>")
Latitude = Mid(Longitude, InStr(Longitude, ",") + 1)
Longitude = Mid(Longitude, 1, InStr(Longitude, ",") - 1)
End If
'combine all the now distinctly workable elements and display em
Temp = "Country: " & Country & " (" & CountryShort & ")" & vbCrLf
Temp += "Region/City: " & City & vbCrLf & "State: " & State & vbCrLf
Temp += "Longitude: " & Longitude & vbCrLf & "Latitude: " & Latitude & vbCrLf
txtHostIP.Text = Temp ' regular asp textbox
End If
- - - - - -
Function GetNode(ByRef rawXML As String, ByVal Target As String) As StringDim X, Y As Integer, CloseTag As String
X = InStr(rawXML, Target) + Len(Target) ' get start position of target value into X
CloseTag = "</" & Mid(Target, 2) 'create matching closing tag
Y = InStr(rawXML, CloseTag) - X 'start of closing tag minus X = Length of value
Return Mid(rawXML, X, Y) ' return specified target value End Function
- - - - - - - - - -
Since I hate following message threads that never result in a posted working answer I wanted to throw my semi-final "if it works don't fix it" solution in for anybody who may be following this... :-)
Thanks again! :-)