サーバでWebサイトにアクセスするには?
サーバ上で、異なるサーバへHTTPアクセスを行うことができる、ServerXMLHTTPを調べたのでメモ。チョットまとめ。
概要
ServerXMLHTTP は、サーバー間で HTTP アクセスを行うためのメソッドとプロパティを提供してくれます。
使い方
詳しくはMSDN参照。レガシーASPの記述方法が載っています(JScript)。Using ServerXMLHTTP Directly | Microsoft Docs
<% Dim objSrvHTTP Set objSrvHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0") Call objSrvHTTP.open("GET", "http://example.com/hoge.html", True) Call objSrvHTTP.send() Response.ContentType = "text/html" Response.Write( objSrvHTTP.responseText ) Set objSrvHTTP = Nothing %>
- 応答が完了するまでキッチリ待つのであれば、Send 後に以下を実行するようにする
While objXMLHttp.readyState <> 4 objXMLHttp.waitForResponse 1000 Wend
ServerXMLHTTPメンバー
英語しかない・・・。ところどころ訳してみました。注脚は原文の説明です。
IServerXMLHTTPRequest-ServerXMLHTTP Members | Microsoft Docs
プロパティ
メソッド
- abort()
- 要求のキャンセル *9
- getAllResponseHeaders()
- HTTPヘッダを取得する(全て) *10
- getOption(option)
- オプション値取得 *11
option:下記オプション値参照 - getResponseHeader(bstrHeader)
- HTTPヘッダを取得する(名前指定) *12
bstrHeader:ヘッダ名 - open(bstrMethod, bstrUrl, [bAsync], [bstrUser], [bstrPassword])
- 要求を初期化し、方法(GET/POSTなど)、URL、認証方法を設定します。 *13
bstrMethod:HTTPメソッド
bstrUrl:リクエストURL。"http://〜"や、"../MyPath/hoge.htm"
bAsync:規定値は False。同期(True)か、非同期(False)
bstrUser:認証(ユーザ名)。Nullや空文字はログインダイアログが表示される
bstrPassword:認証(パスワード) - send(varBody)
- 要求の送信 *14
varBody:要求時に送るボディー部。POSTする際などに使用。 - setOption(option, value)
- オプション値設定 *15
option:下記オプション値参照
value:オプションに設定する値 - setProxy(proxySetting, varProxyServer, varBypassList)
- プロキシ設定 *16
proxySetting:0(デフォルト)、1(ダイレクト)、2(プロキシ経由)
varProxyServer:サーバ名
varBypassList:ダイレクトに接続するホスト名やIPアドレス - setProxyCredentials(username, password)
- プロキシの証明書設定 *17
username:プロキシ利用の為のユーザ名
password:プロキシ利用の為のパスワード - setTimeouts(resolveTimeout, connectTimeout, sendTimeout, receiveTimeout)
- タイムアウト設定 *18
- setRequestHeader(bstrHeader, bstrValue)
- HTTPヘッダの設定 *19
bstrHeader:ヘッダ名
bstrValue:ヘッダ値
既定値のUser-Agentは、"Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)" などとなるようです(最後の数字はバージョン?)。書き換え可能。 - waitForResponse([timeoutInSeconds])
- 応答待機時間 *20
timeoutInSeconds:同期時の待ち時間[ミリ秒]
オプション値
- -1:SXH_OPTION_URL
- リソースのURLを含む文字列型のバリアントを返します。
- 0:SXH_OPTION_URL_CODEPAGE
- デフォルトの CP_UTF8 は、(open関数に指定された) Unicode URL文字列をシングルバイト表現に変換するために用いられたコードページです。
- 1:SXH_OPTION_ESCAPE_PERCENT_IN_URL
- 既定値は、ANSI文字を含むURLをエスケープしません。(" " -> "%20")
- 2:SXH_OPTION_IGNORE_SERVER_SSL_CERT_ERROR_FLAGS
- SSL証明書でエラーを検知した場合の動作を設定できます。既定値は、全てのエラーを無視します。
- 3:SXH_OPTION_SELECT_CLIENT_SSL_CERT
- ???
*1:Specifies the event handler to be invoked when the readyState property changes. Read/write.
*2:Represents the response entity body as an array of unsigned bytes. Read-only.
*3:Represents the state of the request. Read-only.
*4:Represents the response entity body as an IStream. Read-only.
*5:Represents the response entity body as a string. Read-only.
*6:Represents the response entity body as parsed by MicrosoftR XML Core Services (MSXML). Read-only.
*7:Represents the HTTP status code returned by a request. Read-only.
*8:Represents the HTTP response line status. Read-only.
*9:Cancels the current HTTP request.
*10:Retrieves the values of all the HTTP headers.
*11:Returns the value of one of the following options:
*12:Retrieves the value of an HTTP header from the response body.
*13:Initializes a request and specifies the method, URL, and authentication information for the request.
*14:Sends an HTTP request to the server and receives a response.
*15:Sets one of the following options:
*16:Specify proxy configuration.
*17:Specify proxy authentication credentials.
*18:Specifies timeout settings for resolving the domain name, establishing the connection to the server, sending the data, and receiving the response.
*19:Specifies the name of an HTTP header.
*20:Allows the requesting server to suspend execution while waiting for an asynchronous send operation to complete.