C#如何解析http报文

2019-12-26 13:12:28刘景俊
  • Content_MD5 = 36,  Content_Range = 37, 
  • ETag = 38,  Expires = 39, 
  • Last_Modified = 40,  Location = 41, 
  • Proxy_Authenticate = 42,  Refresh = 43, 
  • Retry_After = 44,  Server = 45, 
  • Set_Cookie = 46,  Trailer = 47, 
  • Transfer_Encoding = 48,  Vary = 49, 
  • Warning = 50,  WWW_Authenticate = 51 
  • };  class HTTPHeader 
  • {  #region PROPERTIES 
  • private string[] m_StrHTTPField = new string[52];  private byte[] m_byteData = new byte[4096]; 
  • public string[] HTTPField  { 
  • get { return m_StrHTTPField; }  set { m_StrHTTPField = value; } 
  • }  public byte[] Data 
  • {  get { return m_byteData; } 
  • set { m_byteData = value; }  } 
  • #endregion  // convertion 
  • System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();  #region CONSTRUCTEUR 
  • /// <summary>  /// Constructeur par défaut - non utilisé 
  • /// </summary>  private HTTPHeader() 
  • { }  public HTTPHeader(byte[] ByteHTTPRequest) 
  • {  string HTTPRequest = encoding.GetString(ByteHTTPRequest); 
  • try  { 
  • int IndexHeaderEnd;  string Header; 
  • // Si la taille de requête est supérieur ou égale à 1460, alors toutes la chaine est l'entête http  if (HTTPRequest.Length <= 1460) 
  • Header = HTTPRequest;  else 
  • {  IndexHeaderEnd = HTTPRequest.IndexOf("rnrn"); 
  • Header = HTTPRequest.Substring(0, IndexHeaderEnd);  Data = ByteHTTPRequest.Skip(IndexHeaderEnd + 4).ToArray(); 
  • }  HTTPHeaderParse(Header);