使用api函数URLDownloadToFilePrivate Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As LongPrivate Function getHtmlStr2$(strUrl$) Const tmpfile As String = "c:\tmp.tmp" URLDownloadToFile 0, strUrl, tmpfile, 0, 0 Do Until FileLen(tmpfile) DoEvents Loop Dim fn%, tmp$ fn = FreeFile Open tmpfile For Binary Access Read As #fn tmp = Space(FileLen(tmpfile)) Get #fn, , tmp Close #fn getHtmlStr2 = tmp End Function
我下面这样,好像可以获取,不出错。 Private Sub Command1_Click() Command1.Enabled = False GetHtmlStr Text1 End SubPrivate Sub GetHtmlStr(strUrl As String) Dim xml As Object
Const tmpfile As String = "c:\tmp.tmp"
URLDownloadToFile 0, strUrl, tmpfile, 0, 0
Do Until FileLen(tmpfile)
DoEvents
Loop
Dim fn%, tmp$
fn = FreeFile
Open tmpfile For Binary Access Read As #fn
tmp = Space(FileLen(tmpfile))
Get #fn, , tmp
Close #fn
getHtmlStr2 = tmp
End Function
Private Sub Command1_Click()
Command1.Enabled = False
GetHtmlStr Text1
End SubPrivate Sub GetHtmlStr(strUrl As String)
Dim xml As Object
Set xml = CreateObject("msxml2.serverxmlhttp")
xml.Open "GET", strUrl, False
xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xml.setRequestHeader "Accept-Language", "zh-cn"
xml.setRequestHeader "Accept-Encoding", "gzip, deflate"
xml.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; SV1; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
xml.send
Do While xml.readyState <> 4
DoEvents
Loop
Text2 = StrConv(xml.ResponseBody, vbUnicode)
Set xml = Nothing
Command1.Enabled = True
End Sub
太棒了,就是我想要的,我看也是使用的get方法,好像原理一样,只是引用的对象不一样,具体参数也不一样。
我在网上搜了好几天,居然都没有见过你这个方法,api那个见过,但是还是喜欢xmlhttp。能给讲解一下吗,或者给一个相关的链接教程,我补充一下知识。非常非常感谢呀。
因为该页面要跳转几次,所以xmlhttp会老是提示拒绝访问。还有个简单的方法,就是用Inet控件,一句话搞定:Text2 = Inet1.OpenURL("http://detail.tmall.com/item.htm?id=25854064127")
太厉害了,这个跳转是怎么得到的呀,真是太崇拜你了。另外,inet不行吧,我第一开始就是用的inet,只能得到部分代码。
太厉害了,这个跳转是怎么得到的呀,真是太崇拜你了。另外,inet不行吧,我第一开始就是用的inet,只能得到部分代码。inet我试过了,可以的呀。 跳转是用"WinHttp.WinHttpRequest.5.1"的getResponseHeader("Location")获得的。