作者 主題: [分享]使用C# .net 在網路上的應用  (閱讀 4846 次)

0 會員 與 1 訪客 正在閱讀本文。

小徒兒

  • 鑽研的研究生
  • *****
  • 文章數: 622
    • 檢視個人資料
[分享]使用C# .net 在網路上的應用
« 於: 2005-12-30 11:38 »
***ftp
Socket serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);


***DNS
using System.Net;
using System.Net.Sockets;


      private void Page_Load(object sender, System.EventArgs e)
      {
         
         IPHostEntry IPHost = Dns.GetHostByName("www.microsoft.com");

         int IPv4Count = 0;
         int IPv6Count = 0;

         foreach(IPAddress Address in IPHost.AddressList)
         {
            if (Address.AddressFamily == AddressFamily.InterNetwork)
            {
               IPv4Count++;
               Response.Write("IPv4 Address #" + IPv4Count.ToString() + " is " +
                  Address.ToString()+"</br>");
            }
            else if (Address.AddressFamily == AddressFamily.InterNetworkV6)
            {
               IPv6Count++;
               Response.Write("IPv6 Address #" + IPv6Count.ToString() + " is " +
                  Address.ToString()+"</br>");
            }
         }


      }




***抓取網頁資料

using System.Net;
using System.IO;



public HttpWebRequest   httpRequest;
      public HttpWebResponse  httpResponse;
      StreamReader streamReader = null;

      private void Page_Load(object sender, System.EventArgs e)
      {
         
         httpRequest = (HttpWebRequest) WebRequest.Create("http://tw.stock.yahoo.com/q/q?s=2498");
         httpResponse =(HttpWebResponse) httpRequest.GetResponse();

         if (httpResponse.ContentType.StartsWith(@"text"))
         {

            streamReader= new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("BIG5"));
            string httpContent= streamReader.ReadToEnd();

            Response.Write(httpContent);
 
         }




//ps. 讀取簡體中文 System.Text.Encoding.GetEncoding("GB18030")