Used to obtain the webpage class (go! VB.net reference)

Source: Internet
Author: User

Imports system. net
Imports system. Io

Public class httpdriverclass httpdriver

Public Function getpage () function getpage (byval URL as string, optional byref postpara as string = "", optional byref enctype as string = "gb2312") as string
Return getpage (URL, postpara, nothing, false, enctype)
End Function

Public Function getpage () function getpage (byval URL as string, byref postpara as system. Collections. hashtable, optional byref enctype as string = "gb2312") as string
Return getpage (URL, coltostr (postpara), enctype)
End Function

Public Function getpage () function getpage (byval URL as string, byref postpara as string, byref cookies as cookiecollection, byval hascookie as Boolean, optional byref enctype as string = "gb2312 ", optional byref refer as string = "") as string
If (URL. startswith ("http: //") = false) then
Url = "http: //" & URL
End if
Dim hrqst as httpwebrequest = httpwebrequest. Create (URL)
If (hascookie = true andalso (not cookies is nothing) then
Hrqst. cookiecontainer = new cookiecontainer
Hrqst. cookiecontainer. Add (cookies)
End if

Hrqst. contenttype = "application/X-WWW-form-urlencoded"
Hrqst. headers. Add ("Accept-language", "ZH-CN ")
Dim streamdata as stream
Dim Bt () as byte
If (postpara = "") then
Hrqst. method = "get"
Else
Hrqst. method = "Post"
Hrqst. allowwritestreambuffering = true
Bt = system. Text. encoding. ASCII. getbytes (postpara)
Hrqst. contentlength = Bt. Length
Hrqst. useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0 )"
Hrqst. Referer = refer
Hrqst. keepalive = false
Hrqst. Timeout = 20000
Streamdata = hrqst. getrequeststream ()
Streamdata. Write (BT, 0, BT. length)
Streamdata. Close ()
End if
Dim HRSP as httpwebresponse
HRSP = hrqst. getresponse ()
Streamdata = HRSP. getresponsestream ()
If (hascookie = true andalso (not HRSP. Cookies is nothing) then
Cookies. Add (HRSP. Cookies)
End if
If (enctype = "") then
Enctype = "gb2312"
End if
Dim readstream as new IO. streamreader (streamdata, system. Text. encoding. getencoding (enctype ))
Getpage = readstream. readtoend ()
Streamdata. Close ()
End Function

Public Function getpage () function getpage (byval URL as string, byref postpara as system. collections. hashtable, byref cookies as cookiecollection, byval hascookie as Boolean, optional byref enctype as string = "gb2312") as string
Return getpage (URL, coltostr (postpara), cookies, true, enctype)
End Function

Public Function getpage () function getpage (byval URL as string, byref cookies as cookiecollection, byval hascookie as Boolean, optional byref enctype as string = "gb2312") as string
Return getpage (URL, "", cookies, true, enctype)
End Function

'This function is used to convert the form item set to a string
Public shared function coltostr () function coltostr (byref HT as system. Collections. hashtable, optional byref enctype as string = "gb2312") as string
Dim STR as string
Dim para as dictionaryentry
For each para in HT
STR & = system. Web. httputility. urlencode (ctype (para. Key, string), text. encoding. getencoding (enctype ))
STR & = "="
STR & = system. Web. httputility. urlencode (ctype (para. Value, string), text. encoding. getencoding (enctype ))
STR & = "&"
Next
STR = Str. substring (0, str. Length-1)
Return Str
End Function

End Class

If you need to support cookies and refer, you can use the httpdriver class below.

'This class is used to access pages containing cookies
Imports system. Io

Public class useragentclass useragent
Private m_cookies as new system. net. cookiecollection
Private refer as string
Private HD as new httpdriver

Public Function getpage () function getpage (byval URL as string, optional byref postpara as string = "", optional byref enctype as string = "gb2312") as string
Getpage = HD. getpage (URL, postpara, m_cookies, true, enctype, refer)
Refer = URL
End Function

Public Function getpage () function getpage (byval URL as string, byref postpara as hashtable, optional byref enctype as string = "gb2312") as string
Return getpage (URL, HD. coltostr (postpara), enctype)
End Function

Public Function setcookie () function setcookie (byval cookies as system. net. cookiecollection)
M_cookies = cookies
End Function

Public Function getcookie () function getcookie () as system. net. cookiecollection
Return m_cookies
End Function
End Class

Simple get Functions
Public Function getpage () function getpage (byval URL as string) as string
Dim hrqst as httpwebrequest = httpwebrequest. Create (URL)

Hrqst. contenttype = "application/X-WWW-form-urlencoded"
Hrqst. method = "get"
Dim streamdata as stream

Dim HRSP as httpwebresponse = hrqst. getresponse ()
Streamdata = HRSP. getresponsestream ()

Dim readstream as new IO. streamreader (streamdata, system. Text. encoding. getencoding ("gb2312 "))
Getpage = readstream. readtoend ()
Streamdata. Close ()
End Function

Get method:
Dim UA as new useragent
Dim content as string
Dim URL as string
Url = "www.sina.com.cn"
Content = UA. getpage (URL)

POST method:
Dim UA as new useragent
Dim content as string
Dim URL as string
Dim HT as new hashtable

Url = "mail.sina.com.cn"
Ht. Add ("username", "username ")
Ht. Add ("password", "password ")
Content = UA. getpage (URL, HT)

Related Article

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.