0

当我运行下面的代码时,我无法获取响应文本和返回的响应正文。“HTTP/1.1 200 OK”消息与响应标头一起返回,但没有响应正文。我已经使用 Fiddler2 确认了这个结果,并且还查看了 netsh 跟踪日志。

例如,其他 URL(http://real-chart.finance.yahoo.com/table.csv?s=CELG&d=6&e=26&f=2014&g=d&a=2&b=26&c=1990&ignore=.csv)返回响应文本为以及响应机构。

为什么这个 URL 有问题,我怎样才能让它返回响应正文?

Sub testlogin()

    fileUrl = "http://financials.morningstar.com/ajax/ReportProcess4CSV.html?t=XNYS:HFC&region=USA&culture=en-US&productCode=COM&reportType=is&period=&dataType=A&order=desc&columnYear=5&rounding=3&view=raw"

    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")

    WHTTP.Open "GET", fileUrl, False

    WHTTP.Send

    MsgBox WHTTP.Status
    MsgBox WHTTP.ResponseText
    MsgBox WHTTP.ResponseBody
    MsgBox WHTTP.GetAllResponseHeaders

    Set WHTTP = Nothing

End Sub
4

1 回答 1

0

GET您是否研究过对两个 URL 的调用返回的那些响应标头?

晨星是这样的:

Cache-Control: max-age=0
Connection: keep-alive
Date: Sat, 26 Jul 2014 22:07:33 GMT
Pragma: no-cache
Content-Length: 0
===>> Content-Type: text/html;charset=UTF-8 <<===
===>> Content-Encoding: gzip <<===
Server: Apache
Set-Cookie: JSESSIONID=6FAF41A612ABB32B0C670AB07BF0D8A5; HttpOnly
Vary: User-Agent
Vary: Accept-Encoding
com.coradiant.appvis: vid=ad&sid=CONTROLLER_1&tid=da615c36-2a18-4129-bcd7-1cbb139ab52b
Content-Disposition: attachment;filename=""HFC Income Statement.csv""
ExpiresDefault: access plus 2 hours

雅虎财经是这样的:

Cache-Control: private
Connection: close
Date: Sat, 26 Jul 2014 22:10:00 GMT
Transfer-Encoding: chunked
===>> Content-Type: text/csv <<===
P3P: policyref=""http://info.yahoo.com/w3c/p3p.xml"", CP=""CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV""
Set-Cookie: B=d3svnbl9t89po&b=3&s=4i; expires=Tue, 26-Jul-2016 22:10:00 GMT; path=/; domain=.yahoo.com
Vary: Accept-Encoding

我已经高亮了Content-TypeContent-Encoding标题(如果有的话)。

基本上,两个调用返回的内容是不同的。显然,Excel 可以解释第二种情况,其中内容类型是“text/csv”,但第一种情况是一个奇怪的 gzipped html 页面,我猜 Excel 无法理解。

我不可能为您提供解决此问题的方法,但标题的内容肯定可以解释您所看到的行为差异。

于 2014-07-26T22:20:55.863 回答