Decrypt the Gzip compressed Web page data stream (GO)

Source: Internet
Author: User
Tags decrypt

The page data cannot be retrieved because a webpage is experiencing problems.

After a few troubleshooting, found that the site will check the client header information, if you encounter an ambiguous header information is directly negative, return 0 data.

If the header information is correct, the gzip-compressed data is returned, so the idea of getting the Web data directly is dashed.

As a matter of fact. NET has encapsulated the GZIP and deflate-decompression algorithm classes for us, all in the System.IO.Compression namespace.

Let me show you how to parse and extract the gzip-compressed web data stream.

Imports System.Net
Imports System.IO
Imports System.IO.Compression
Imports System.Text



Function gzip2html (ByVal url asstring) asstring

' HTTP request
Dim req as HttpWebRequest = httpwebrequest.create (URL)
Req. Method = "Get" ' Get ' or POST
Req. Accept = "*/*"
Req. useragent = "mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;) "' Here simulates the request field of the XP system IE7
Req. Referer = "Source Address"

' A simulation of some header information
Req. Headers ("accept-language") = "ZH-CN"
Req. Headers ("ua-cpu") = "x86"
Req. Headers ("accept-encoding") = "gzip, deflate"

' HTTP GET
Dim res as HttpWebResponse = req. GetResponse
' Get Data text encoding
' Dim enc as Encoding = encoding.getencoding (res. CharacterSet) ' If there is garbled, please change the following way
Dim enc as Encoding = Encoding.default

' Create a gzip decompression stream
Dim GZ asnew GZipStream (res. GetResponseStream, Compressionmode.decompress)
' Use a temporary memory stream to save the extracted data
Dim Ms Asnew MemoryStream
' Buffered data
Dim buf (asbyte), I asinteger=0
' Constantly extracting data from the stream
Whiletrue
i = GZ. Read (buf, 0, 100)
If I =0thenexitwhile
Ms. Write (buf, 0, i)
Endwhile
' Convert data to Characters
DIM ret asstring= Enc. GetString (Ms. ToArray)

' Close all the streams
Gz. Close ()
Ms. Close ()
Res. Close ()

Return ret
End Function



I test the decompression data successfully, other environment is not tested, please modify it yourself.

Decrypt the Gzip compressed Web page data stream (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.