2013-03-03 82 views
2

这里是一个函数的源代码我使用的进一步等待处理获取HTML代码:VBA WINHTTP没有映射字符

Public Function DownloadTextFile(url As String) As String 
    Dim oHTTP As WinHttp.WinHttpRequest 

    Set oHTTP = New WinHttp.WinHttpRequest 
    oHTTP.Open Method:="GET", url:=url, async:=False 
    oHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" 
    'oHTTP.setRequestHeader "Content-Type", "multipart/form-data; " 
    oHTTP.setRequestHeader "Content-Type", "text/html; charset=utf-8" 
    oHTTP.Option(WinHttpRequestOption_EnableRedirects) = True 
    oHTTP.send 

    Dim success As Boolean 
    success = oHTTP.waitForResponse() 
    If Not success Then 
     Debug.Print "DOWNLOAD FAILED!" 
     Exit Function 
    End If 

    Dim responseText As String 
    Debug.Print oHTTP.responseText 

    responseText = oHTTP.responseText 
    'Set fs = CreateObject("Scripting.FileSystemObject") 
    'Set a = fs.CreateTextFile("c:\testfile.txt", True, False) 
    'Set a = fs.CreateTextFile("c:\testfile.txt", True, True) 
    'a.WriteLine oHTTP.responseText 
    'a.Close 

    Set oHTTP = Nothing 

    DownloadTextFile = responseText 
End Function 

它适用于大多数的网页,但对于一些页面responseTextNo Mapping for the Unicode character exists in the target multi-byte code page

这里是网页的一个例子的量,responseTextNo Mapping for the Unicode character exists in the target multi-byte code page

http://bzp0.portal.uzp.gov.pl/index.php?ogloszenie=browser&action=search&rodzajzamowienia=B&rodzajogloszenia=1&aktualne=1&datapublikacji_rodzaj=5&iloscwynikownastronie=20&offset=20

,这里是一个可疑人物不能被编码(截图来自谷歌浏览器):

http://imageshack.us/photo/my-images/585/errsource.png/

时不时在同一个网站上,但对于不同的搜索结果这个功能不会产生错误,但是,然后,在immidi的HTML源代码吃的窗口就像?????? ...

任何想法如何使它的工作?

回答

1

尝试使用中StrConv:

DownloadTextFile = VBA.Strings.StrConv(responseText, vbUnicode) 

vbUnicode:使用系统的默认代码页的字符串为Unicode转换。 (在Macintosh上不可用。)

+0

丹尼尔你好。我已检查了您的建议,并提供了有关问题解决方案的一些进展。 – user2127981 2013-03-03 16:34:49

+0

对于几个网页与屏幕截图中的字符,debuger会在代码中标记“responseText = oHTTP.responseText”部分代码,而oHTTP.responseText则包含“No mapping character ...” – user2127981 2013-03-03 16:48:46

+0

您是否尝试过:responseText = StrConv (oHTTP.responseText,vbUnicode)? – dee 2013-03-03 17:17:54

1

解决方案,为我工作:

responseText = VBA.Strings.StrConv(oHTTP.ResponseBody, vbUnicode) 

的使用注意事项ResponseBody的,而不是responseText的