[Original] Post to obtain resources returned from the Internet

Source: Internet
Author: User

Post gets resources returned from the Internet, which is written in a post on the forum.


'================================================ ============
'
'Posttofile ("http://entry.126.com/cgi/login ",_
'"<Base href =" "http://m134.mail.126.com/"> ",_
'"User = xxx & pass = xxx", "xxx.html ")
'
'Power by: landlordh
'Note:
'The original copyright information should be retained for reprinting. Please do not use it for illegal channels
'The related folder permissions are not considered in this function.
'================================================ ==============

Private cookies as net. cookiecontainer = nothing

Private sub posttofile (byval httpurl as string, byval basehref as string, byval poststr as string, byval filename as string)
Try
Dim code as system. Text. Encoding = system. Text. encoding. getencoding ("gb18030") '"big5"
Dim URL as new system. Uri (httpurl)

Dim req as net. httpwebrequest = ctype (net. webrequest. Create (URL), net. httpwebrequest)
Dim somebyte () as byte = code. getbytes (poststr)
Req. keepalive = false
Req. method = "Post"
Req. contenttype = "application/X-WWW-form-urlencoded"
'Req. useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; q312461;. Net CLR 1.0.3705 )"
Req. contentlength = somebyte. Length
If not (Me. Cookies is nothing) then
Req. cookiecontainer = me. Cookies
Else
Req. cookiecontainer = new net. cookiecontainer
Me. Cookies = Req. cookiecontainer
End if
Dim writestream as Io. Stream = Req. getrequeststream
Writestream. Write (somebyte, 0, somebyte. length)
Writestream. Close ()
Dim res as net. httpwebresponse = ctype (req. getresponse, net. httpwebresponse)
Dim receivestream as Io. Stream = res. getresponsestream
Dim readstream as streamreader = new streamreader (receivestream, code)
Dim getstr as string
Dim read (256) as [char]
Dim count as integer = readstream. Read (read, 0,256)
While count> 0
Dim STR as new [String] (read, 0, count)
Getstr & = Str
Count = readstream. Read (read, 0,256)
End while
Readstream. Close ()
Receivestream. Close ()
Req. Abort ()
Res. Close ()
Getstr = getstr. insert (0, basehref) 'If a relative path is used, control the base href of the target. Otherwise, the webpage cannot be opened.
Try
If filename. Trim. Length = 0 or filename = nothing then
'No file is returned
Else
If Io. file. exists (filename) then Io. file. Delete (filename)
Dim SR as new IO. streamwriter (Io. file. Create (filename), Code)
'Dim SR as Io. streamwriter = Io. file. createtext (filename) 'Encoding compatibility (utf8)
Sr. Write (getstr)
Sr. Close ()
End if
Catch e as exception
Msgbox (E. Message)
End try
Catch
End try
End sub

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.