1

Web からクエリを実行して Access データベースを更新するマクロを作成しようとしています。何らかの理由で、VBA は http を友好的に扱うことを拒否していますが、https を使用することに完全に満足しています。

ここに私の要求機能があります:

Function httpRequest(ByVal url As String, useProxy As Boolean) As String

    Dim response As String
    Dim proxy As String
    Dim xhr As Object


    'Make HTTP requester object
    Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")

    If useProxy Then
        If Left(url, 5) = "https" Then
            proxy = "proxyb1secure:8443"
        Else
            proxy = "proxyb1:8080"
        End If
        xhr.setProxy 2, proxy
    End If

    xhr.Open "GET", url, False

    'send the request. THIS LINE TIMES OUT ON HTTP
    xhr.Send
    'fetch the whole page
    response = xhr.responseText

    'clean up
    Set xhr = Nothing

    'return
    httpRequest = response

End Function

そして私のテスト機能:

Function testProxy()
    'This one works
    MsgBox (httpRequest("https://www.bing.com/search?q=stackoverflow", True))
    'This one doesn't.
    MsgBox (httpRequest("http://www.bing.com/search?q=stackoverflow", True))
End Function

私はJavaで同じことをテストしたので、正しい名前とポートを求めていると確信しています.

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.bing.com/search?q=stackoverflow");
    HttpURLConnection con = (HttpURLConnection) url.openConnection(getProxyForURL(url));
    System.out.println(con.getResponseCode() + " " + con.getResponseMessage());
    InputStream is = con.getInputStream();
    int c;
    StringBuilder sb = new StringBuilder();
    while ((c = is.read()) != -1) {
        sb.append((char) c);
    }
    String page = sb.toString();
    System.out.println(page);
}

public static Proxy getProxyForURL(URL url) {
    if (url.getProtocol().equals("https")) {
        return new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyb1secure", 8443));
    } else {
        return new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyb1", 8080));
    }
}

私が見逃している VBA のトリッキーは何ですか?

4

1 に答える 1

1

謎が解けました。これは、ユーザーエージェントを中心に展開するセキュリティ機能であることがわかりました(すべて...)。

Java は次の HTTP ヘッダーを使用しました (成功しました)。

GET http://www.bing.com/search?q=stackoverflow HTTP/1.1
Accept: */*
Accept-Language: en-us
Proxy-Connection: Keep-Alive
User-Agent: Java/1.7.0_79
Host: www.bing.com

アクセスはこれらを送信しました(失敗しました):

GET http://www.bing.com/search?q=stackoverflow HTTP/1.1
Accept: */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
Proxy-Connection: Keep-Alive
Host: www.bing.com

を追加するだけで

xhr.setRequestHeader "User-Agent", "PayMeNoAttention"

それは魔法のように通り抜けます。理論を確認するために、Access のユーザー エージェントを Java に追加すると、Java が失敗しました。そう。それは間違いなくどうなっています。

これは、マクロ ウイルスが悪意のあるサイトに接触するのをブロックするための、優れたネットワーク技術者による試みである可能性があります。

于 2015-07-13T15:57:09.730 に答える