为什么某些站点不能使用服务器端脚本(ASP)通过XMLHTTPREQUEST进行加载,但是我可以使用javascript进行加载?

时间:2019-06-08 19:58:59

标签: asp-classic xmlhttprequest

我正在尝试通过xmlhttprequest用Classic ASP加载以下测试url。但是其中两个站点将无法加载。我知道该脚本有效,因为我可以运行某些站点,但某些站点无法加载。有什么解释吗?

我已经用Javascript加载了这些站点,并且确实加载了(不包括代码,但是标准的AJAX或纯JS脚本)。那么为什么客户端脚本而不是服务器端代码(ASP)可以工作?

'rss_url = "https://www.nationalgeographic.com/science/2019/06/opal-fossils-reveal-new-species-dinosaur-australia-fostoria" 'THiS URL DOES NOT LOAD
rss_url = "https://www.nbcnews.com/news/us-news/ex-minneapolis-officer-who-killed-justine-damond-sentenced-12-5-n1013926" 'THIS URL DOES NOT LOAD
'rss_url = "https://www.reuters.com/article/us-usa-saudi-arms/republican-democratic-senators-seek-to-block-trump-saudi-arms-sales-idUSKCN1T61PL" 'THIS URL LOADS

Dim objHTTP
Set objHTTP = Server.CreateObject("MSXML2.XMLHTTP.6.0")
    Err.Clear ' shouldn't be needed; can't hurt
    ON ERROR RESUME NEXT
objHTTP.Open "GET", rss_url, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
'objHTTP.setRequestHeader "Content-type", "text/html"
 aErr = Array(Err.Number, Err.Description)
   On Error GoTo 0
    If 0 = aErr(0) Then
      On Error Resume Next
objHTTP.Send
aErr = Array(Err.Number, Err.Description)
      On Error GoTo 0
       Select Case True
         Case 0 <> aErr(0)
           response.write "send failed:" &  aErr(0) & aErr(1)
         Case 200 = objHTTP.status
           response.write rss_url & objHTTP.status & objHTTP.statusText
         Case Else
           response.write "further work needed:"
           response.write rss_url & objHTTP.status & objHTTP.statusText
       End Select
 Else
       response.write "open failed:" & aErr(0) & aErr(1)
    End If       

    'ON ERROR GOTO 0
    If Err.Number <> 0 Then
        Response.Write "NO feed from ..."
    end if
    if objHTTP.Status = 200 Then sdata = BinaryToString(objHTTP.ResponseBody)
     response.write sdata & "<hr>"
Set objHTTP = Nothing

Function BinaryToString(byVal Binary)
    '--- Converts the binary content to text using ADODB Stream

    '--- Set the return value in case of error
    BinaryToString = ""

    '--- Creates ADODB Stream
    Dim BinaryStream
    Set BinaryStream = CreateObject("ADODB.Stream")

    '--- Specify stream type.
    BinaryStream.Type = 1 '--- adTypeBinary

    '--- Open the stream And write text/string data To the object
    BinaryStream.Open
    BinaryStream.Write Binary

    '--- Change stream type to text
    BinaryStream.Position = 0
    BinaryStream.Type = 2 '--- adTypeText

    '--- Specify charset for the source text (unicode) data.
    BinaryStream.CharSet = "UTF-8"

    '--- Return converted text from the object
    BinaryToString = BinaryStream.ReadText
End Function 

1 个答案:

答案 0 :(得分:0)

尝试这个(from this answer)

Function GetTextFromUrl(url)
  Dim oXMLHTTP
  Dim strStatusTest
  Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
  oXMLHTTP.Open "GET", url, False
  oXMLHTTP.Send
  If oXMLHTTP.Status = 200 Then
    GetTextFromUrl = oXMLHTTP.responseText
  End If
End Function

Dim sResult : sResult = GetTextFromUrl("https://www.nbcnews.com/news/us-news/ex-minneapolis-officer-who-killed-justine-damond-sentenced-12-5-n1013926")
Response.Write sResult

对我来说很好,抓住页面并显示它。