网络编程--System.Net
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; namespace dns类 { public partial class Form1 : Form { public Form1() { InitializeComponent(); label5.Text = string.Empty; } private void button1_Click(object sender, EventArgs e) { if(textBox1.Text == string.Empty) { MessageBox.Show("请输入IP地址"); } else { textBox2.Text = string.Empty; textBox3.Text = string.Empty; textBox4.Text = string.Empty; IPAddress[] ips = Dns.GetHostAddresses(textBox1.Text); foreach(IPAddress ip in ips) { textBox2.Text = ip.ToString(); label5.Text += "网际协议地址:" + ip.Address + "\nIp地址的地址族:" + ip.AddressFamily.ToString() + "\n是否是IPv6连接本地地址:" + ip.IsIPv6LinkLocal; } textBox3.Text = Dns.GetHostName(); // textBox4.Text = Dns.GetHostByName(Dns.GetHostName()).HostName; } } private void label4_Click(object sender, EventArgs e) { } } }
WebRequest 和 WebResponse类的使用
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.IO; namespace dns类 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { richTextBox1.Text = string.Empty; //创建WebRequest对象 WebRequest webreq = WebRequest.Create(textBox1.Text); //设置用于对Internet资源请求进行身份验证的网络凭证 webreq.Credentials = CredentialCache.DefaultCredentials; //调用WebRequest对象的各种属性获取WebRequest请求的相关信息 richTextBox1.Text += "请求数据内容长度:" + webreq.ContentLength; richTextBox1.Text += "\n该请求的协议方法:" + webreq.Method; richTextBox1.Text += "\n访问Internet的网络代理:" + webreq.Proxy; richTextBox1.Text += "\nInternet URI:" + webreq.RequestUri; richTextBox1.Text += "\n超时:" + webreq.Timeout; //创建WebResponse对象 WebResponse webres = webreq.GetResponse(); richTextBox1.Text += "\n相应请求的URI" + webres.ResponseUri; //创建StreamReader流读取对象 Stream stream = webres.GetResponseStream(); StreamReader sreader = new StreamReader(stream); richTextBox1.Text += "\n" + sreader.ReadToEnd(); sreader.Close(); stream.Close(); webres.Close(); } } }