标签:
class Program{static void Main(string[] args){WebClient web = new WebClient();byte[] temp = web.DownloadData("http://www.baidu.com");//下载URI资源文件string Response = Encoding.UTF8.GetString(temp);//解码Console.WriteLine(Response);Console.Read();}}
class Program{static void Main(string[] args){WebClient web = new WebClient();Stream st = web.OpenRead("http://www.baidu.com");//打开URI资源流StreamReader sr = new StreamReader(st);string Response = sr.ReadToEnd();Console.WriteLine(Response);Console.Read();}}
class Program{static void Main(string[] args){WebClient web = new WebClient();string str = "李志伟";byte[] response = web.UploadData("http://www.baidu.com", Encoding.Default.GetBytes(str));Console.WriteLine(Encoding.Default.GetString(response));//解码Console.Read();}}
class Program{static void Main(string[] args){WebRequest req = WebRequest.Create("http://www.baidu.com");//得到请求对象WebResponse res = req.GetResponse();//得到相应对象Stream strm = res.GetResponseStream();//得到相应数据流StreamReader sr = new StreamReader(strm);Console.WriteLine(sr.ReadToEnd());Console.Read();}}

class Program{static void Main(string[] args){string uri = "http://www.baidu.com";HttpWebRequest httpRe = (HttpWebRequest)HttpWebRequest.Create(uri);HttpWebResponse httpRes = (HttpWebResponse)httpRe.GetResponse();Stream strm = httpRes.GetResponseStream();//得到相应数据流StreamReader sr = new StreamReader(strm);Console.WriteLine(sr.ReadToEnd());Console.Read();}}
class Program{static void Main(string[] args){WebRequest req = WebRequest.Create("http://www.baidu.com");//得到请求对象NetworkCredential cred = new NetworkCredential("userName", "password");req.Credentials = cred;//调用GetResponse()之前赋值WebResponse res = req.GetResponse();//得到相应对象Stream strm = res.GetResponseStream();//得到相应数据流StreamReader sr = new StreamReader(strm);Console.WriteLine(sr.ReadToEnd());Console.Read();}}
class Program{static void Main(string[] args){WebRequest req = WebRequest.Create("http://www.baidu.com");//得到请求对象NetworkCredential cred = new NetworkCredential("userName", "password","Domain");WebProxy wp = new WebProxy("192.168.1.100", true);//设置代理服务器地址req.Credentials = cred;//调用GetResponse()之前赋值req.Proxy = wp;//调用GetResponse()之前赋值WebResponse res = req.GetResponse();//得到相应对象Stream strm = res.GetResponseStream();//得到相应数据流StreamReader sr = new StreamReader(strm);Console.WriteLine(sr.ReadToEnd());Console.Read();}}
class Program{static void Main(string[] args){WebRequest req = WebRequest.Create("http://www.baidu.com");//得到请求对象req.BeginGetResponse(new AsyncCallback(Callback), req);//得到相应对象Console.WriteLine("异步请求已发送...");Console.Read();}//回调函数private static void Callback(IAsyncResult ar){Thread.Sleep(5000);//休眠5秒WebRequest req = (WebRequest)ar.AsyncState;WebResponse res = req.EndGetResponse(ar);Stream strm = res.GetResponseStream();//得到相应数据流StreamReader sr = new StreamReader(strm);Console.WriteLine(sr.ReadToEnd());}}


private void button1_Click(object sender, EventArgs e)//按钮事件{this.webBrowser1.Navigate("http://www.baidu.com");//加载文档}
class Program{static void Main(string[] args){Uri uri = new Uri("http://www.baidu.com/s?wd=URI");//只读属性,Uri对象被创建后就不能修改了Console.WriteLine(uri.Query);//获取指定URI中包括的任何查询信息:?wd=URIConsole.WriteLine(uri.AbsolutePath);//获取 URI 的绝对路径:/sConsole.WriteLine(uri.Scheme);//获取此 URI 的方案名称:httpConsole.WriteLine(uri.Port);//获取此 URI 的端口号:80Console.WriteLine(uri.Host);//获取此实例的主机部分:www.baidu.comConsole.WriteLine(uri.IsDefaultPort);//URI的端口值是否为此方案的默认值:true//创建UriBuilder对象,并给其属性赋值UriBuilder urib = new UriBuilder();urib.Host = uri.Host;urib.Scheme = uri.Scheme;urib.Path = uri.AbsolutePath;urib.Port = uri.Port;//测试UriTest(uri);//使用Uri对象UriTest(urib.Uri);//使用UriBuilder对象Console.Read();}//测试Uri对象static void UriTest(Uri uri){Console.WriteLine("==============" + uri + "开始===============");Thread.Sleep(5000);HttpWebRequest httpweb = (HttpWebRequest)HttpWebRequest.Create(uri);HttpWebResponse res = (HttpWebResponse)httpweb.GetResponse();Stream stream = res.GetResponseStream();StreamReader strread = new StreamReader(stream);Console.WriteLine(strread.ReadToEnd());Console.WriteLine("======================完成===================\n\n\n\n");}}
class Program{static void Main(string[] args){//IPAddress类提供了对IP地址的转换、处理等功能IPAddress ip =IPAddress.Parse("202.108.22.5");//百度的IPbyte[] bit = ip.GetAddressBytes();foreach (byte b in bit){Console.Write(b+" ");}Console.WriteLine("\n"+ip.ToString()+"\n\n");//IPHostEntry类的实例对象中包含了Internet主机的相关信息IPHostEntry iphe = Dns.GetHostEntry("www.microsoft.com");Console.WriteLine("www.microsoft.com主机DNS名:" + iphe.HostName);foreach (IPAddress address in iphe.AddressList){Console.WriteLine("关联IP:"+address);}Console.WriteLine("\n");//Dns类提供了一系列静态的方法,用于获取提供本地或远程域名等功能iphe = Dns.GetHostEntry(Dns.GetHostName());Console.WriteLine("本地计算机名:" + iphe.HostName);foreach (IPAddress address in iphe.AddressList){Console.WriteLine("关联IP:" + address);}Console.Read();}}
class Program{static void Main(string[] args){Encoding utf8 = Encoding.UTF8;Encoding gb2312 = Encoding.GetEncoding("GB2312");//编码string test = "编码解码测试 ABCDabcd!";char[] source = test.ToCharArray();int len = utf8.GetByteCount(source, 0, test.Length);byte[] result = new byte[len];utf8.GetBytes(source, 0, test.Length, result, 0);foreach (byte b in result){Console.Write("{0:X}", b);//16进制显示}Console.WriteLine();//解码Console.WriteLine(utf8.GetString(result));//输出字符串//得到所有系统编码EncodingInfo[] encodings = Encoding.GetEncodings();foreach (EncodingInfo e in encodings){Console.WriteLine(e.Name);}//将URI地址进行编码Console.WriteLine(Uri.EscapeUriString("http://www.baidu.com/s?wd=李志伟"));//使用HttpUtility类进行编码与解码string temp = HttpUtility.UrlEncode("李志伟", gb2312);Console.WriteLine(temp + "-->" + HttpUtility.UrlDecode(temp, gb2312));Console.Read();}}
class Program{static void Main(string[] args){Thread.Sleep(1000 * 2);Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//服务器的IP和端口IPEndPoint ie = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);client.Connect(ie);//连接服务端byte[] data = new byte[1024];int recv = client.Receive(data);//接收消息Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));for (int i = 0; i < 20; i++){client.Send(Encoding.UTF8.GetBytes(i + ":李志伟发送的消息!\n"));Console.WriteLine("发送消息数:" + i);Thread.Sleep(1000);}client.Shutdown(SocketShutdown.Both);//断开连接client.Close();Console.WriteLine("连接已断开!");Console.Read();}}
class Program{static void Main(string[] args){IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);sock.Bind(ipep);//设置监听地址sock.Listen(10);//监听Console.WriteLine("已监听...");while (true){Socket client = sock.Accept();//连接客户端Console.WriteLine("连接成功:" + client.RemoteEndPoint.ToString() + " -->" + client.LocalEndPoint.ToString());byte[] data = Encoding.UTF8.GetBytes("欢迎!!!");client.Send(data, data.Length, SocketFlags.None);//给客户端发送信息//不断的从客户端获取信息while (client.Connected){data = new byte[1024];int recv = client.Receive(data);Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));}client.Close();}sock.Close();}}
class Program{static void Main(string[] args){Thread.Sleep(1000 * 2);for (int i = 0; i < 20; i++){TcpClient tcpClient = new TcpClient();tcpClient.Connect(IPAddress.Parse("127.0.0.1"), 9999);Console.WriteLine(i + ":连接成功!");NetworkStream ns = tcpClient.GetStream();//打开流Byte[] sendBytes = Encoding.UTF8.GetBytes(i + ":李志伟发送的消息!\n");ns.Write(sendBytes, 0, sendBytes.Length);ns.Dispose();//释放流tcpClient.Close();//释放连接Console.WriteLine("已发送消息数:" + i);Thread.Sleep(1000);}Console.Read();}}
class Program{static void Main(string[] args){IPAddress ip = IPAddress.Parse("127.0.0.1");TcpListener listener = new TcpListener(ip, 9999);//IP地址与端口号listener.Start();// 开始侦听Console.WriteLine("已开始侦听...");while (true){TcpClient client = listener.AcceptTcpClient();//接受挂起的连接请求Console.WriteLine("连接成功{0}-->{1}", client.Client.RemoteEndPoint.ToString(), client.Client.LocalEndPoint.ToString());NetworkStream ns = client.GetStream();StreamReader sread = new StreamReader(ns);Console.WriteLine(sread.ReadToEnd());}}}
class Program{static void Main(string[] args){Thread.Sleep(1000 * 2);UdpClient udpClient = new UdpClient();udpClient.Connect(IPAddress.Parse("127.0.0.1"), 9999);//连接for (int i = 0; i < 20; i++){Byte[] data = Encoding.UTF8.GetBytes(i + ":李志伟发送的消息!");udpClient.Send(data, data.Length);//发送数据Console.WriteLine("发送消息数:" + i);Thread.Sleep(1000);}udpClient.Close();Console.Read();}}
class Program{static void Main(string[] args){UdpClient udpClient = new UdpClient(9999);Console.WriteLine("已监听...");IPEndPoint RemoteIpEndPoint = null;while (true)//不断地接收数据{byte[] data = udpClient.Receive(ref RemoteIpEndPoint);//接收数据string str = Encoding.UTF8.GetString(data);Console.WriteLine("收到消息:" + str);Console.WriteLine("消息来源:" + RemoteIpEndPoint.ToString()+"\n");}}}
class Program{static void Main(string[] args){SmtpClient smtp = new SmtpClient();MailMessage mail = new MailMessage("XXX@163.com", "XXXXXX@qq.com");//图像附件Attachment attach = new Attachment(@"F:\图片.jpg", MediaTypeNames.Image.Jpeg);//设置ContentIdattach.ContentId = "pic";//ZIP附件Attachment attach2 = new Attachment(@"F:\图片.rar", "application/x-zip-compressed");mail.Attachments.Add(attach);//添加附件mail.Attachments.Add(attach2);//添加附件//标题和内容,注意设置编码,因为默认编码是ASCIImail.Subject = "你好";mail.SubjectEncoding = Encoding.UTF8;//HTML内容mail.Body = "<img src=\"cid:pic\"/><p>来自李志伟。</p>";mail.BodyEncoding = Encoding.UTF8;//指示改电子邮件内容是HTML格式mail.IsBodyHtml = true;//SMTP设置(根据邮箱类型设置,这里是163 Mail的SMTP服务器地址)smtp.Host = "smtp.163.com";smtp.UseDefaultCredentials = false;//某些SMTP服务器可能不支持SSL,会抛出异常smtp.EnableSsl = true;smtp.Credentials = new NetworkCredential("XXX@163.com", "password");smtp.DeliveryMethod = SmtpDeliveryMethod.Network;//发送smtp.Send(mail);Console.WriteLine("=============OK=============");Console.Read();}}
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4315911.html