次の方法で共有


Sys.Net.WebRequest クラス

更新 : 2007 年 11 月

Web 要求を行うためのクライアント スクリプト機能を提供します。

名前空間 :Sys.Net

継承 : なし

var wRequest = new Sys.Net.WebRequest();

コンストラクタ

名前

説明

Sys.Net.WebRequest の webRequest コンストラクタ

WebRequest クラスの新しいインスタンスを初期化します。

メンバ

名前

説明

Sys.Net.WebRequest の add_completed メソッド

Web 要求インスタンスに関連付けるイベント ハンドラを登録します。

Sys.Net.WebRequest の completed メソッド

関連付けられた Sys.Net.WebRequest インスタンスの完了イベントを発生させます。

Sys.Net.WebRequest.getResolvedUrl メソッド

Web 要求インスタンスの解決された URL を取得します。

Sys.Net.WebRequest の invoke メソッド

Web 要求インスタンスのネットワーク呼び出しを発行します。

Sys.Net.WebRequest の remove_completed メソッド

Web 要求インスタンスに関連付けられたイベント ハンドラを削除します。

Sys.Net.WebRequest の body プロパティ

Web 要求の HTTP 本文を取得または設定します。

Sys.Net.WebRequest の executor プロパティ

関連付けられた Web 要求インスタンスのエグゼキュータを取得または設定します。

Sys.Net.WebRequest の headers プロパティ

Web 要求の HTTP ヘッダーを取得します。

Sys.Net.WebRequest の httpVerb プロパティ

Web 要求の発行に使用される Web 要求 HTTP 動詞を取得または設定します。

Sys.Net.WebRequest の timeout プロパティ

Web 要求インスタンスのタイムアウト値を取得または設定します。

Sys.Net.WebRequest の url プロパティ

Web 要求インスタンスの URL を取得または設定します。

Sys.Net.WebRequest の userContext プロパティ

Web 要求インスタンスに関連付けられているユーザー コンテキストを取得または設定します。

解説

WebRequest クラスは、Web 要求を行うために非同期通信レイヤ クラスによって使用されます。この型は、通常アプリケーションでは直接使用しません。代わりに、プロキシ メソッドを使用して、ブラウザから Web サービス メソッドまたは静的ページ メソッドを呼び出します。詳細については、「クライアント スクリプトへの Web サービスの公開」を参照してください。

ただし、次のシナリオでは WebRequest インスタンスを作成して使用できます。

  • 単純ではない Web サービス メソッド呼び出しでネットワーク要求を行う場合。

  • HTTP 要求プロパティを直接設定する場合。

  • Sys.Net.WebRequestExecutor クラスに基づくカスタム エグゼキュータを使用している場合。

使用例

WebRequest クラスのインスタンスを作成して GET Web 要求を行う方法を次の例に示します。この例には、Web ページや WebRequest クラスとの対話に使用されるクライアント スクリプトが示されています。

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">

        <title> Using WebRequest </title>

        <style type="text/css">
            body {  font: 11pt Trebuchet MS;
                    font-color: #000000;
                    padding-top: 72px;
                    text-align: center }

            .text { font: 8pt Trebuchet MS }
        </style>


     </head>

   <body>

    <h2>Using WebRequest</h2>

         <form id="form1" runat="server">
            <asp:ScriptManager runat="server" ID="ScriptManagerId">
                <Scripts>
                    <asp:ScriptReference Path="WebRequest.js" />
                </Scripts>
            </asp:ScriptManager>
        </form>










        <table>
            <tr align="left">
                <td>Make GET Request:</td>
                <td>
                    <button id="Button1"  
                        onclick="GetWebRequest()">GET</button>
                </td>
            </tr>
            <tr align="left">  
                <td>Request Body:</td>
                <td>
                    <button id="Button2"  
                        onclick="PostWebRequest()">Body</button>
                </td>
            </tr>
            <tr align="left">
                <td>Request Timeout:</td>
                <td>
                    <button id="Button3"  
                        onclick="WebRequestTimeout()">Timeout</button>
                </td>
            </tr> 
            <tr align="left">
                <td>Request Completed Handler:</td>
                <td>
                    <button id="Button4"  
                        onclick="WebRequestCompleted()">Completed Handler</button>
                </td>
            </tr>
            <tr align="left">
                <td>Resolved Url:</td>
                <td>
                    <button id="Button5"  
                        onclick="GetWebRequestResolvedUrl()">Resolved Url</button>
                </td>
            </tr>
            <tr align="left">
                <td>Request Executor:</td>
                <td>
                    <button id="Button6"  
                        onclick="WebRequestExecutor()">Executor</button>
                </td>
            </tr>
            <tr align="left">
                <td>Request Header:</td>
                <td>
                    <button id="Button7"  
                        onclick="WebRequestHeader()">Header</button>
                </td>
            </tr>
        </table>



        <hr />

        <div id="ResultId" style="background-color:Aqua;"></div>



    </body>

</html>

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">

        <title> Using WebRequest </title>

        <style type="text/css">
            body {  font: 11pt Trebuchet MS;
                    font-color: #000000;
                    padding-top: 72px;
                    text-align: center }

            .text { font: 8pt Trebuchet MS }
        </style>

   </head>

   <body>

    <h2>Using WebRequest</h2>

        <form id="form1" runat="server">
            <asp:ScriptManager runat="server" ID="ScriptManagerId">
                <Scripts>
                    <asp:ScriptReference Path="WebRequest.js" />
                </Scripts>
            </asp:ScriptManager>
        </form>









        <table>
            <tr align="left">
                <td>Make GET Request:</td>
                <td>
                    <button id="Button1"  
                        onclick="GetWebRequest()">GET</button>
                </td>
            </tr>
            <tr align="left">  
                <td>Request Body:</td>
                <td>
                    <button id="Button2"  
                        onclick="PostWebRequest()">Body</button>
                </td>
            </tr>
            <tr align="left">
                <td>Request Timeout:</td>
                <td>
                    <button id="Button3"  
                        onclick="WebRequestTimeout()">Timeout</button>
                </td>
            </tr> 
            <tr align="left">
                <td>Request Completed Handler:</td>
                <td>
                    <button id="Button4"  
                        onclick="WebRequestCompleted()">Completed Handler</button>
                </td>
            </tr>
            <tr align="left">
                <td>Resolved Url:</td>
                <td>
                    <button id="Button5"  
                        onclick="GetWebRequestResolvedUrl()">Resolved Url</button>
                </td>
            </tr>
            <tr align="left">
                <td>Request Executor:</td>
                <td>
                    <button id="Button6"  
                        onclick="WebRequestExecutor()">Executor</button>
                </td>
            </tr>
            <tr align="left">
                <td>Request Header:</td>
                <td>
                    <button id="Button7"  
                        onclick="WebRequestHeader()">Header</button>
                </td>
            </tr>
        </table>



        <hr />

        <div id="ResultId" style="background-color:Aqua;"></div>



    </body>

</html>
var getPage;
var postPage;
var displayElement;

function pageLoad()
{
    getPage = "getTarget.htm";
    postPage = "postTarget.aspx";
    displayElement = $get("ResultId");
}

// This function performs a GET Web request 
// to retrieve  information from the Url specified in 
// the query string. 
function GetWebRequest()
{
    alert("Performing Get Web request.");

    // Instantiate the WebRequest object.
    var wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url(getPage);  

    // Set the request verb.
    wRequest.set_httpVerb("GET");

    // Set user's context
    wRequest.set_userContext("user's context");

    // Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted);


    // Clear the results page element.
    displayElement.innerHTML = "";

    // Execute the request.
    wRequest.invoke();  

}

// This function performs a POST Web request
// to upload information to the resource 
// identified by the Url. 
function PostWebRequest()
{
    // Instantiate the WebRequest object.
    var wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url(postPage); 

    // Set the request verb.
    wRequest.set_httpVerb("POST");

    var body = "Message=Hello! Do you hear me?"
    wRequest.set_body(body);
    wRequest.get_headers()["Content-Length"] = body.length;


    // Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted);

    // Clear the results page element.
    displayElement.innerHTML = "";

    // Execute the request.
    wRequest.invoke();  
}

// This function adds and removes the 
// Web request completed event handler.
function WebRequestCompleted()
{
    // Instantiate the WebRequest.
    var wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url(getPage);  

    // Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted);   
    alert("Added Web request completed handler");

    // Remove the web request completed event handler.
    // Comment the following two lines if you want to
    // use the handler.
    wRequest.remove_completed(OnWebRequestCompleted); 
    alert("Removed handler; the Web request return is not processed.");

    // Execute the request.
    wRequest.invoke();  
}

// This function gets the resolved Url 
// of the Web request instance.
function GetWebRequestResolvedUrl()
{
    // Instantiate the WebRequest.
    var wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url(getPage);  

    // Get the web request completed event handler.
    var resUrl = wRequest.getResolvedUrl();   
    alert("Resolved Url: " + resUrl);

    // Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted); 

    // Execute the request.
    wRequest.invoke();  

}


// This function gets and sets the 
// Web request time out.
function WebRequestTimeout()
{    
    // Instantiate the WebRequest.
    var wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url(getPage);  

    var defaultTimeout =  
        wRequest.get_timeout();

    // Set request timeout to 100 msec.
    wRequest.set_timeout(100);

    var newTimeout = 
        wRequest.get_timeout();

    alert("Default timeout: " + defaultTimeout);
    alert("New timeout: " + newTimeout);

    // Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted);   

    // Execute the request.
    wRequest.invoke();     
}


// This function sets the Web request
// executor, replacing the default one.
function WebRequestExecutor()
{    
    // Instantiate the WebRequest.
    var wRequest =  new Sys.Net.WebRequest();

    // Create the executor. In this case it is an
    // XMLHttpExecutor, equivalent to the default
    // executor. But, you can create a custom one.
    var executor = new Sys.Net.XMLHttpExecutor();

    // Set the executor, replacing the default one. 
    // In this case the executor is equivalent to the
    // default one.
    wRequest.set_executor(executor); 

    // Get the current executor       
    var executor =  
        wRequest.get_executor();

    alert("Response availabe: " + executor.get_responseAvailable())
}

 // This function sets an HTTP header for
 // the Web request.
 function WebRequestHeader() 
 {
       // Instantiate the WebRequest object.
    var wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url(postPage); 

    // Set the request verb.
    wRequest.set_httpVerb("POST");

    var body = "Message=Hello! Do you hear me?"
    wRequest.set_body(body);

    // Set the value of the HTTP header's "Content-Length".
    wRequest.get_headers()["Content-Length"] = body.length;

    // Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompletedHeader);

    // Clear the results page element.
    displayElement.innerHTML = "";

    // Execute the request.
    wRequest.invoke();  
}

// This the handler for the Web request completed event
// that is used to display return data.
function OnWebRequestCompleted(executor, eventArgs) 
{
    if(executor.get_responseAvailable()) 
    {

        // Clear the previous results. 
        displayElement.innerHTML = "";

        // Display Web request status.                  
        DisplayWebRequestStatus(executor);

        // Display Web request headers.                  
        DisplayWebRequestHeaders(executor);

        // Display Web request body.                  
        DisplayWebRequestBody(executor);

    }
    else
    {
        if (executor.get_timedOut())
            alert("Timed Out");
        else
            if (executor.get_aborted())
                alert("Aborted");
    }
}


// This the handler for the Web request completed event
// that is used to display header information.
function OnWebRequestCompletedHeader(executor, eventArgs) 
{
    if(executor.get_responseAvailable()) 
    {

        // Clear the previous results. 
         displayElement.innerHTML = "";

        // Display Web request headers.                  
        DisplayWebRequestHeaders(executor);

    }
    else
    {

        if (executor.get_timedOut())
            alert("Timed Out");

        else

            if (executor.get_aborted())
                alert("Aborted");

    }
}

// This function is used to display the Web request status.
function DisplayWebRequestStatus(executor)
{
     displayElement.innerHTML +=
     "Status: [" + 
     executor.get_statusCode() + " " + 
     executor.get_statusText() + "]" + "<br/>"
}

// This function is used to display Web request HTTP headers.
function DisplayWebRequestHeaders(executor)
{
    displayElement.innerHTML += 
        "Headers: ";
    displayElement.innerHTML += 
        executor.getAllResponseHeaders() + "<br/>";
 }

// This function is used to display the Web request body.
function DisplayWebRequestBody(executor)
{   
     displayElement.innerHTML += 
        "Body: ";
    if (document.all)
         displayElement.innerText += 
            executor.get_responseData();
    else
        // Firefox
         displayElement.textContent += 
            executor.get_responseData();
}

// This function is used to display the Web request message.
function DisplayInformation(message)
{
    // Clear the previous results.
     displayElement.innerHTML = "";
    // Display information.
    displayElement.innerHTML = "<br/>" + message;
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

参照

参照

Sys.Net.XMLHttpExecutor クラス

Sys.Net.WebRequestManager クラス