Solved

Finding IP Address of site visitor

Posted on 2013-06-13
3
273 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
  • 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now