Monday, 29 October 2012

Get External Valid IP Address

Get External Valid IP Address


Here is a method to find out the external Valid IP address of your network/Connection:

In Design Part Include a label and Button:

        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Include these namespaces first:

using System.IO;
using System.Net;
using System.Text;  

Then include this method in your codebehind:

        public static string GetExternalIP(string Provider)




                if ((Provider == null) || (Provider == ""))

                    Provider = "";

                HttpWebRequest WebReq = (HttpWebRequest)HttpWebRequest.Create(Provider);

                HttpWebResponse WebRes = (HttpWebResponse)WebReq.GetResponse();

                System.IO.Stream ResStream = WebRes.GetResponseStream();

                StreamReader ResStreamReader = new StreamReader(ResStream, Encoding.UTF8);

                string IP = ResStreamReader.ReadToEnd();



               return IP;


            catch (Exception ex)


                return "";




Then On the button click Event:

    protected void Button1_Click(object sender, EventArgs e)


        string IPinternet = GetExternalIP(null);



No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top