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:
<div> <asp:Label ID="Label1" runat="server"></asp:Label> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div>
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) { try { if ((Provider == null) || (Provider == "")) Provider = "http://automation.whatismyip.com/n09230945.asp"; 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(); ResStream.Close(); WebRes.Close(); return IP; } catch (Exception ex) { return "127.0.0.1"; //throw; } }
Then On the button click Event:
protected void Button1_Click(object sender, EventArgs e) { string IPinternet = GetExternalIP(null); Label1.Text=IPinternet; }
No comments:
Post a Comment
Thank You for Your Comments. We will get back to you soon.