一段完整的Socket HTTP協議中 GET報文的應用

適合剛接觸socket和http的人

註釋比較全面

方法的返回數據是我爲了方便調用並取得調用結果的

如果不需要可以去掉

GETHTML方法返回的數據就是取得的網頁HTML代碼

可以利用字符串拆分 /r/n/r/n 注意是兩個哦

分出報頭和內容

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net.Sockets;
  5. using System.Net;
  6. using System.IO;
  7. namespace 貼吧江湖
  8. {
  9.     public class clsSocket
  10.     {
  11.         private Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //創建socket 連接
  12.         IPHostEntry IpHost;
  13.         public int CreatSocket(string strUrl, int intPort)
  14.         {
  15.             try
  16.             {
  17.                 IpHost = Dns.Resolve(strUrl);//解析主機地址
  18.             }
  19.             catch (Exception)
  20.             {
  21.                 return 5;
  22.             }
  23.             IPAddress ip = IpHost.AddressList[0];       //獲取主機IP
  24.             IPEndPoint ipe = new IPEndPoint(ip, intPort);   //創建IPEndPoint實例
  25.             try
  26.             {
  27.                 s.Connect(ipe);//嘗試連接 
  28.             }
  29.             catch (ArgumentNullException ae) //處理參數爲空引用異常  
  30.             {
  31.                 Console.WriteLine("ArgumentNullException : {0}", ae.ToString());
  32.                 return 1;
  33.             }
  34.             catch (SocketException se) //處理操作系統異常  
  35.             {
  36.                 Console.WriteLine("SocketException : {0}", se.ToString());
  37.                 return 2;
  38.             }
  39.             catch (Exception es)
  40.             {
  41.                 Console.WriteLine("Unexpected exception : {0}", es.ToString());
  42.                 return 3;
  43.             }
  44.             return 0;
  45.         }
  46.         public string GetHtml()
  47.         {
  48.             string sendStr = "GET /bbs/ HTTP/1.1/r/n";
  49.             sendStr += "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*/r/n";
  50.             sendStr += "Accept-Language: zh-cn/r/n";
  51.             sendStr += "Host: " + IpHost.HostName + "/r/n";
  52.             sendStr += "Connection: Keep-Alive/r/n/r/n";//創建報文頭
  53.             byte[] bytesSendStr = new byte[1024];// 創建bytes字節數組以轉換髮送串  
  54.             bytesSendStr = Encoding.ASCII.GetBytes(sendStr); //將發送內容字符串轉換成字節byte數組 
  55.             try
  56.             {
  57.                 s.Send(bytesSendStr, bytesSendStr.Length, 0);//向主機發送請求 
  58.             }
  59.             catch (Exception ce)
  60.             {
  61.                 return ce.Message;
  62.             }
  63.             string recvStr = ""//聲明接收返回內容的字符串  
  64.             byte[] recvBytes = new byte[1024];//聲明字節數組,一次接收數據的長度爲1024字節
  65.             int bytes = 0;//返回實際接收內容的字節數,循環讀取,直到接收完所有數據
  66.             while (true)
  67.             {
  68.                 bytes = s.Receive(recvBytes, recvBytes.Length, 0);
  69.                 if (bytes <= 0)//讀取完成後退出循環 
  70.                     break;
  71.            
  72.                 Encoding gb2312 = Encoding.GetEncoding("gb2312");//將讀取的字節數轉換爲字符串  
  73.                 recvStr += gb2312.GetString(recvBytes, 0, bytes);
  74.             }
  75.       
  76.             return recvStr;
  77.         }
  78.         public void CloseSocket()
  79.         {
  80.             s.Shutdown(SocketShutdown.Both);
  81.             s.Close();
  82.         }
  83.     }
  84. }

 

寫入文本的代碼

 

  1.   //將所讀取的字符串轉換爲字節數組 
  2.     byte[] content = Encoding.ASCII.GetBytes(recvStr);
  3.     try
  4.     {
  5.         //創建文件流對象實例 
  6.         FileStream fs = new FileStream("fileName.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  7.         //寫入文件 
  8.         fs.Write(content, 0, content.Length);
  9.     }
  10.     catch (Exception fe)
  11.     {
  12.         MessageBox.Show("文件創建/寫入錯誤:" + fe.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
  13.     }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章