sbmzhcn
2014-10-24 09:09:27 +08:00
'''
#region page source analytics
private void AnalyPageSource(MemoryStream ms,
out WebHeaderCollection responseHeaders,
out string pageContent)
{
byte[] bytes = ms.ToArray();
StringBuilder string_buffer = new StringBuilder();
byte[] contentBytes = new byte[bytes.Length];
responseHeaders = new WebHeaderCollection();
pageContent = string.Empty;
// process headers
for (int i = 0; i < bytes.Length; i++)
{
string_buffer.Append((char)bytes[i]);
if (string_buffer.ToString().ToLower().IndexOf("\r\n\r\n") != -1)
{
int index = string_buffer.ToString().IndexOf("\r\n\r\n");
string originalheaderString = string_buffer.ToString().Substring(0, index);
Header = originalheaderString;
//Array.Copy(bytes, index + 4, contentBytes, 0, ms.Length - index - 4);
Array.Copy(bytes, index + 4, contentBytes, 0, bytes.Length - index - 4);
string originalPageContent = Encoding.UTF8.GetString(contentBytes);
string responseState = "";
string[] headers = originalheaderString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in headers)
{
if ( item.StartsWith( "Set-Cookie:", StringComparison.OrdinalIgnoreCase ))
{
string tCookie = item.Substring(11, item.IndexOf(";") < 0 ? item.Length - 11 : item.IndexOf(";") - 10).Trim();
if ( !this.Cookies.Exists( f => f.Split( '=' )[0] == tCookie.Split( '=' )[0] ) )
{
this.Cookies.Add( tCookie );
}
}
int colonIndex = item.IndexOf(":");
if (colonIndex > -1)
responseHeaders.Add(item.Substring(0, colonIndex).Trim(), item.Substring(colonIndex + 1).Trim());
else
responseState = item;
}
StatusCode = responseState.Split(' ')[1];
if (responseState.IndexOf(" 302 ") != -1 || responseState.IndexOf(" 301 ") != -1)
{
if (responseHeaders["Location"] != null)
{
try { ResponseUri = new Uri(responseHeaders["Location"]); }
catch { ResponseUri = new Uri(ResponseUri, responseHeaders["Location"]); }
}
}
ContentType = Headers["Content-Type"];
if (Headers["Content-Length"] != null)
ContentLength = int.Parse(Headers["Content-Length"]);
KeepAlive = (Headers["Connection"] != null && Headers["Connection"].ToLower() == "keep-alive") ||
(Headers["Proxy-Connection"] != null && Headers["Proxy-Connection"].ToLower() == "keep-alive");
try
{
if (!String.IsNullOrEmpty(responseHeaders[HttpResponseHeader.TransferEncoding]))
{
if (responseHeaders[HttpResponseHeader.TransferEncoding].Contains("chunked"))
{
contentBytes = ChunkedDecompress(contentBytes);
}
}
}
catch (Exception ex)
{
throw new Exception("处理chunked数据失败:" + ex);
}
try
{
if (!String.IsNullOrEmpty(responseHeaders[HttpResponseHeader.ContentEncoding]))
{
if (responseHeaders[HttpResponseHeader.ContentEncoding].Contains("gzip")
|| responseHeaders[HttpResponseHeader.ContentEncoding].Contains("deflate"))
{
pageContent = GzipDecompress(contentBytes);
}
}
else
{
pageContent = Encoding.UTF8.GetString(contentBytes);
}
}
catch (Exception ex)
{
throw new Exception("GzipDecompress Error: " + ex.Message);
}
if (string.IsNullOrEmpty(pageContent) && !string.IsNullOrEmpty(originalPageContent))
{
pageContent = originalPageContent;
}
break;
}
}
}
#endregion
'''
这是我代码中的一部分,为什么我没觉得那么简单呢。