jeudi 26 août 2021

I try to get client public IP from Client but it return server public IP when hosting the website in server. Working fine if run in localhost

Below is my code, when I run in local, it can successfully return my public IP address. But when I host the website to the server, it always returns server public IP address. Does anyone know what is the root cause? I wish to get the public IP address from my client who is using the page I created.

static string GetPublicIPAddress()
{
    string address = "";
    try
    {
        WebRequest request = WebRequest.Create("https://bot.whatismyipaddress.com");
        using (WebResponse response = request.GetResponse())
        using (StreamReader stream = new StreamReader(response.GetResponseStream()))
        {
            address = stream.ReadToEnd();
        }
        
       
    }
    catch(Exception ex) {
        address = ex.Message;
    }
    return address;
}

The server is hosting internally.

I even try using different web addresses as below:

https://api.ipify.org

https://ipinfo.io/ip

https://wtfismyip.com/text

https://checkip.amazonaws.com/

https://bot.whatismyipaddress.com

https://ipecho.net/plain




Aucun commentaire:

Enregistrer un commentaire