首页
社区
课程
招聘
[原创]仿InternetGetCookie自己解析Cookie文件
发表于: 2013-4-26 13:51 13471

[原创]仿InternetGetCookie自己解析Cookie文件

2013-4-26 13:51
13471

主要参考http://blog.csdn.net/whatday/article/details/7566310




[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

上传的附件:
收藏
免费 6
支持
分享
最新回复 (8)
雪    币: 94
活跃值: (465)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
谢谢您的分享,很有意思的代码。不过楼主的代码我编译出来,结果啥都没有。建议楼主发上整个工程代码,上传上来。
2013-4-26 21:12
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
嗯,有意思... 之前也遇到cookie的问题...
2013-4-29 19:57
0
雪    币: 29
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
我刚好前端时间写了一个VB.NET的代码.
也贴上来好了.献丑了.本人不是专科出身,代码也是东抄西拼的.大家伙将就下看吧
Imports System.IO
Imports System.Text

Imports System.Collections.Generic
Imports System.Linq
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Reflection

Public Class BinaryCookieReader1
    Private mybyte() As Byte
    Private outCookie As String = ""
    Public Shared Function ReadFile(ByVal fileName As String) As Byte()
        Dim fs As New FileStream(fileName, FileMode.Open)
        Dim buffer As Byte() = New Byte(fs.Length - 1) {}
        Try
            fs.Read(buffer, 0, buffer.Length)
            fs.Seek(0, SeekOrigin.Begin)
            Return buffer
        Catch
            Return buffer
        Finally
            If fs IsNot Nothing Then
                fs.Close()
            End If
        End Try
    End Function

    Public Function ReadInt32(ByRef myByte As Byte(), ByVal startIndex As Integer, Optional ByVal useLeEndian As Boolean = False) As Integer

        Dim read4Byte(3) As Byte
        If useLeEndian = True Then
            read4Byte(0) = myByte(startIndex + 3)
            read4Byte(1) = myByte(startIndex + 2)
            read4Byte(2) = myByte(startIndex + 1)
            read4Byte(3) = myByte(startIndex + 0)
            ReadInt32 = CInt("&H" + BitConverter.ToString(read4Byte, 0, 4).Replace("-", ""))
        Else
            ReadInt32 = CInt("&H" + BitConverter.ToString(myByte, startIndex, 4).Replace("-", ""))
        End If

    End Function

    Public Function ReadCookies(ByRef mybyte As Byte(), ByVal startIndex As Integer) As String
        '除了
        '1. First 4 bytes in the cookie is the size of the cookie.
        '2. The next 4 bytes are unknown (may be related to cookies flags).
        '3. The next four bytes are the cookie flags. This is an integer value (1=Secure, 4=HttpOnly, 5= Secure+HttpOnly).
        '4. The next 4 bytes are unknown.
        '5. The next 4 bytes is an integer specifying the start of the url field in bytes from the start of the cookie record.
        '6. The next 4 bytes is an integer specifying the start of the name field in bytes from the start of the cookie record.
        '7. The next 4 bytes is an integer specifying the start of the path field in bytes from the start of the cookie record.
        '8. The next 4 bytes is an integer specifying the start of the value field in bytes from the start of the cookie record.
        '9. The next 8 bytes represents the end of the cookie and it is always 0×0000000000000000.
        '10. The next 8 bytes are the cookie expiration date. Date is in Mac epoch format (Mac absolute time). Mac epoch format starts from Jan 2001.
        '11. The next 8 bytes are the cookie creation date.
        '12. Next to that, the cookie contains the actual cookie domain, name, path & value. The order is not specific and they can appear in any order.
        ReadCookies = ""
        Dim CookieLength As Integer = ReadInt32(mybyte, startIndex, True)
        Dim CookieFlag As Integer = ReadInt32(mybyte, startIndex + 8, True)
        Dim CookieUrlOffset As Integer = ReadInt32(mybyte, startIndex + 16, True)
        Dim CookieNameOffset As Integer = ReadInt32(mybyte, startIndex + 20, True)
        Dim CookiePathOffset As Integer = ReadInt32(mybyte, startIndex + 24, True)
        Dim CookieValueOffset As Integer = ReadInt32(mybyte, startIndex + 28, True)
        'end of cookie + 8 0000000000000000
        'Dim ExpirationDate As Long ' end of cookie+8
        'dim CreationDate as long '+8
        Dim CookieUrl As String = Encoding.ASCII.GetString(mybyte, startIndex + CookieUrlOffset, CookieNameOffset - CookieUrlOffset - 1)
        Dim CookieName As String = Encoding.ASCII.GetString(mybyte, startIndex + CookieNameOffset, CookiePathOffset - CookieNameOffset - 1)
        Dim CookiePath As String = Encoding.ASCII.GetString(mybyte, startIndex + CookiePathOffset, CookieValueOffset - CookiePathOffset - 1)
        Dim CookieValue As String = Encoding.ASCII.GetString(mybyte, startIndex + CookieValueOffset, CookieLength - CookieValueOffset - 1)

        outCookie = outCookie + CookieName + ChrW(9) + CookieValue + ChrW(9) + CookiePath + ChrW(9) + CookieUrl + vbCrLf
        ReadCookies = outCookie
    End Function

    Public Function ReadPage(ByRef mybyte As Byte(), ByVal startIndex As Integer)
        'Page(Format)
        '1. Every page starts with a 4 byte page header: 0×00000100.
        '2. Next four bytes is an integer specifying the number of cookies in the page.
        '3. Following that, a 4 byte integer for each cookie, represents the cookie offset. Offset specifies the start of the cookie in bytes from the start of the page.
        '4. Next to that, the page contains the actual cookie contents. Each cookie is of variable length. Cookie format is explained below.
        '5. Page ends with a 4 byte value and it is always 0×00000000.

        Dim PageHeader As Integer = ReadInt32(mybyte, startIndex) '头固定为 0x00000100

        Dim NumberOfCookies As Integer = BitConverter.ToInt32(mybyte, startIndex + 4) 'COOKIE 数量
        'Dim NumberOfCookies As Integer = ReadInt32(mybyte, startIndex + 4) 'COOKIE 数量
        Dim NumberOfCookiesStartIndex As Integer = startIndex + 8
        Dim CookieOffset() As Integer
        ReDim CookieOffset(NumberOfCookies - 1)
        For I As Integer = 0 To NumberOfCookies - 1
            CookieOffset(I) = BitConverter.ToInt32(mybyte, NumberOfCookiesStartIndex + I * 4)
            ReadCookies(mybyte, startIndex + CookieOffset(I))
        Next
    End Function

    Private LengthPage_StartIndex As Integer = 8
    Private Page_StartIndex As Integer = -1

    Public Sub New(ByVal cookiefile As String)
        'FileHead NoOfpages Page1-N    PageHeader  BE Endian

        'PageFormat & Cookie Format  use LE Endian

        mybyte = ReadFile(cookiefile)
        Dim FileHead As String = Encoding.ASCII.GetString(mybyte, 0, 4) ' //0-3

        Dim NumberOfPages As Integer = ReadInt32(mybyte, 4)

        '开始位置就是第9位,索引-1 就是8
        Dim LengthOfPages() As Integer
        ReDim LengthOfPages(NumberOfPages - 1)
        For I As Integer = 0 To NumberOfPages - 1
            LengthOfPages(I) = ReadInt32(mybyte, LengthPage_StartIndex + 4 * I)
        Next

        outCookie = ""

        Dim nextStartIndex As Integer = 8 + NumberOfPages * 4
        For I As Integer = 0 To NumberOfPages - 1
            ReadPage(mybyte, nextStartIndex)
            nextStartIndex = nextStartIndex + LengthOfPages(I)
        Next
        MsgBox(outCookie)

    End Sub
End Class
2013-5-2 19:14
0
雪    币: 284
活跃值: (34)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
mark,mark!!!
2013-5-2 19:53
0
雪    币: 276
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
代码很工整。不错。
2013-5-3 06:10
0
雪    币: 206
活跃值: (85)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
代码很详细,值得学习,谢一个!
2013-5-3 11:36
0
雪    币: 81
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
mark a
2013-10-9 00:39
0
雪    币: 231
活跃值: (2631)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
9
mark xia
2013-10-9 01:52
0
游客
登录 | 注册 方可回帖
返回
//