Extracting user's geographical location based on IP address

jason2li

Member
Joined
Nov 18, 2005
Messages
22
Location
St. Louis, MO
Programming Experience
5-10
Hi, I am trying to extract a user's general location based on their IP address. I've found several programs that have an annual fee to accomplish this, but I dug a little deeper and found this:

http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=1.1.1.1&LicenseKey=0 (Change the "1.1.1.1" to a valid IP address)

Which is exactly what I need. It works when I browse to it in IE7. And it also works when I use the exact same code from a Windows application. But when I try to access that page programmatically from my ASP.NET page, it throws this error:

VB.NET:
The remote server returned an error: (401) Unauthorized.

Here is my code:
VB.NET:
    Private Function getGeographicalInformation(ByVal IPAddress As String) As String

        Dim stringBuilder As New System.Text.StringBuilder("")

        Dim request As WebRequest = WebRequest.Create("http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=" & IPAddress & "&LicenseKey=0")

        Dim response As WebResponse = request.GetResponse() [COLOR="Red"]' The error is thrown here[/COLOR]

        Dim iTotalBuff As Integer = 0
        Dim Buffer(128) As [Byte]

        Dim stream As System.IO.Stream = response.GetResponseStream()
        iTotalBuff = stream.Read(Buffer, 0, 128)

        While iTotalBuff <> 0
            stringBuilder.Append(System.Text.Encoding.ASCII.GetString(Buffer, 0, iTotalBuff))
            iTotalBuff = stream.Read(Buffer, 0, 128)
        End While

        Return stringBuilder.ToString.Trim

    End Function

Any ideas?
 
I would have to guess that you do not have access to use that site...Who's web service is that? It is possible that they did not allow anonymous access to their service. It could also be because this service is ASP 1.1 and you are using 2.0?
These are all just shots in the dark. I really hate debugging web application because it can be so many different things.
 
I would have to guess that you do not have access to use that site...Who's web service is that? It is possible that they did not allow anonymous access to their service. It could also be because this service is ASP 1.1 and you are using 2.0?
These are all just shots in the dark. I really hate debugging web application because it can be so many different things.

Thanks for the fast reply... but is it not safe to assume that if I can browse to it, I should be able to program to it? And if I can program to it from a Windows application, shouldn't I be able to program to it from a web application?

Sorry if these are stupid assumptions, I don't know much about web rights.
 
I would think so...Are you adding it as a web reference to your programs?

Possibly this could be the issue...If you add it as a web reference and try to work with it like that rather than how you are I think that it will work. This is how I have always had to integrate web services into my programs.

******Added After******
I was able to add a Web Reference to a web application using the Url provided above to the Asmx file. This contains a few classes and I believe that if you manipulate them you should be able to accomplish what you want. Let me know how things went.
 
Last edited:
I was able to add a Web Reference to a web application using the Url provided above to the Asmx file. This contains a few classes and I believe that if you manipulate them you should be able to accomplish what you want. Let me know how things went.

Sorry, I've never used web references before. I don't really know where to start.

I tried adding a web reference that pointed directly to that url, but it gave me this error:
VB.NET:
The document at the url http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=1.1.1.1&LicenseKey=0 was not recognized as a known document type.
The error message from each known type may help you fix the problem:
- Report from 'DISCO Document' is 'Discovery document at the URL http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=1.1.1.1&LicenseKey=0 could not be found.'.
  - The document format is not recognized.
- Report from 'WSDL Document' is 'There is an error in XML document (2, 2).'.
  - <IPInformation xmlns='http://ws.cdyne.com/'> was not expected.
- Report from 'XML Schema' is 'The root element of a W3C XML Schema should be <schema> and its namespace should be 'http://www.w3.org/2001/XMLSchema'.'.

If you have time, would you mind giving me a little walkthru?
 
add jus tthis path as the web reference:

http://ws.cdyne.com/ip2geo/ip2geo.asmx/

if you do that you will then have access to that objects attributes, properties, classes, and methods. The same way that you would in a regular reference. It is very simple to do you are just trying to add a reference to the wrong path.

Once you have created the reference you can just create an instance of the object (Dim as new) or call its shared methods if necessary. But you should be able to accomplish what you are looking to do.
 
add jus tthis path as the web reference:

http://ws.cdyne.com/ip2geo/ip2geo.asmx/

if you do that you will then have access to that objects attributes, properties, classes, and methods. The same way that you would in a regular reference. It is very simple to do you are just trying to add a reference to the wrong path.

Once you have created the reference you can just create an instance of the object (Dim as new) or call its shared methods if necessary. But you should be able to accomplish what you are looking to do.

This is great! Thanks. It turns out that you have to purchase it to get more than 25 or so lookups, but I can definitely see the benefits of web references! Thanks alot!
 
Back
Top