Solved

Finding IP Address of site visitor

Posted on 2013-06-13
3
285 Views
Last Modified: 2013-06-22
I'm hopeing someone has encountered this before.  I've used this code to get the IP address of a site visitor before with no problems... now I'm adding it to a site that has a table called "Requests" and I am getting an error "Model.Request does not contain a definition for ServerVariables"  My assumption is that the issue is due to there being a table with the same name.  Is there a way around this or a different method of finding the IP address of a site visitor?

Thanks,
Greg


string ip =  Request.ServerVariables["remote_addr"].ToString();

Open in new window

0
Comment
Question by:Greg_L_WER
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 39247083
Hi Greg

The following link has some very good information:
http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net
0
 

Accepted Solution

by:
Greg_L_WER earned 0 total points
ID: 39253896
Hi Angus,

Thanks for the info.. unfortunately none of the options seem to work in my system due to the fact that I have a table called Requests which conflicts with the code.  There was one part of the page that did work though it returns the IP address of the internal PC, not the external IP that I'm needing.  I did uncover a method that does appear to work...


    private string ExternalIPFinder() {
        try {
            WebClient client = new WebClient();
            // Add a user agent header in case the requested URI contains a query.

            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
            string baseurl = "http://checkip.dyndns.org/";
            Stream data = client.OpenRead(baseurl);
            StreamReader reader = new StreamReader(data);
            string IPAddress = reader.ReadToEnd();
            data.Close();
            reader.Close();
            IPAddress = IPAddress
                .Replace("<html><head><title>Current IP Check</title></head><body>", "")
                .Replace("</body></html>", "")
                .Replace("Current IP Address:", "")
                .Replace("\r\n", "").ToString().Trim();

            return IPAddress;
        }
        catch {
            return null;
        }
    }

Open in new window

0
 

Author Closing Comment

by:Greg_L_WER
ID: 39267777
Other solutions provided didn't solve my problem but I came across this information elsewhere and wanted to submit it so it's available for others...
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question