PDA

View Full Version : سوال: بدست آوردن IP اینترنت



NASA's Spaceman
سه شنبه 01 مهر 1393, 10:30 صبح
سلام
میخواستم بدونم چجور میشه آی پی اینترنت رو بدست اورد، نه آی پی محلی
با سپاس Spaceman

omid nasri
سه شنبه 01 مهر 1393, 17:25 عصر
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetPublicIP());
SiteIp();
Console.ReadKey();
}

private static void SiteIp()
{
string howtogeek = "www.facebook.com";
IPAddress[] addresslist = Dns.GetHostAddresses(howtogeek);

foreach (IPAddress theaddress in addresslist)
{
Console.WriteLine(theaddress.ToString());
}
}

static public string GetPublicIP()
{
String direction = "";
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
{
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
direction = stream.ReadToEnd();
}
}

//Search for the ip in the html
int first = direction.IndexOf("Address: ") + 9;
int last = direction.LastIndexOf("</body>");
direction = direction.Substring(first, last - first);

return direction;
}
static private string GetWebServerType()
{
string server = string.Empty;

//get URL
string url = "www.softiran.org"; //txtURL.Text.Trim().ToLower();
if (!url.StartsWith("http://") && !url.StartsWith("https://"))
url = "http://" + url;

HttpWebRequest request = null;
HttpWebResponse response = null;

try
{
request = WebRequest.Create(url) as HttpWebRequest;
response = request.GetResponse() as HttpWebResponse;

// server = response.Headers["Server"];
server = response.Headers["REMOTE_ADDR"];
}
catch (WebException wex)
{
server = "Unknown";
}
finally
{
if (response != null)
{
response.Close();
}
}

return server;
}
}
}

NASA's Spaceman
سه شنبه 01 مهر 1393, 20:59 عصر
ممنون دوست عزیز و من خودم یه جواب خیلی بهتر پیدا کردم که ممکن هست بدرد همه بخوره
اول این using ها مورد نیاز هستن
using System.Net;
using System.Text.RegularExpressions;
و بعد هم این متد رو باید تعریف کرد
public string IP() {
string ip = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
ip = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(ip)[0].ToString();
return ip;
}
فقط کلمه Matches داره بینش تو کد بالا فاصله میفته که خودتون باید فاصله بردارید
و بعد هم خیلی احت فراخونیش کنین مثل کد زیر
private void button1_Click(object sender, EventArgs e) {
MessageBox.Show(IP());
}
با سپاس Spaceman

omid nasri
چهارشنبه 02 مهر 1393, 01:00 صبح
اینی که من نوشتم 3 تابع مجزار هستش