Sys.Net.WebRequest 클래스

업데이트: 2007년 11월

웹 요청을 실행하는 데 필요한 클라이언트 스크립트 기능을 제공합니다.

네임스페이스:Sys.Net

상속: 없음

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

생성자

이름

설명

Sys.Net.WebRequest webRequest 생성자

WebRequest 클래스의 새 인스턴스를 초기화합니다.

멤버

이름

설명

Sys.Net.WebRequest add_completed 메서드

웹 요청 인스턴스와 연결할 이벤트 처리기를 등록합니다.

Sys.Net.WebRequest completed 메서드

연결된 Sys.Net.WebRequest 인스턴스에 대한 완료 이벤트를 발생시킵니다.

Sys.Net.WebRequest.getResolvedUrl 메서드

웹 요청 인스턴스의 확인된 URL을 가져옵니다.

Sys.Net.WebRequest invoke 메서드

웹 요청 인스턴스에 대한 네트워크 호출을 실행합니다.

Sys.Net.WebRequest remove_completed 메서드

웹 요청 인스턴스와 연결된 이벤트 처리기를 제거합니다.

Sys.Net.WebRequest body 속성

웹 요청의 HTTP 본문을 가져오거나 설정합니다.

Sys.Net.WebRequest executor 속성

연결된 웹 요청 인스턴스의 실행자를 가져오거나 설정합니다.

Sys.Net.WebRequest headers 속성

웹 요청에 대한 HTTP 헤더를 가져옵니다.

Sys.Net.WebRequest httpVerb 속성

웹 요청을 발생시키는 데 사용되는 웹 요청 HTTP 동사를 가져오거나 설정합니다.

Sys.Net.WebRequest timeout 속성

웹 요청 인스턴스의 제한 시간 값을 가져오거나 설정합니다.

Sys.Net.WebRequest url 속성

웹 요청 인스턴스의 URL을 가져오거나 설정합니다.

Sys.Net.WebRequest userContext 속성

웹 요청 인스턴스와 연결되는 사용자 컨텍스트를 가져오거나 설정합니다.

설명

WebRequest 클래스는 비동기 통신 계층 클래스에서 웹 요청을 실행하는 데 사용됩니다. 일반적으로 응용 프로그램에서 직접 이 형식을 사용하지 않습니다. 대신 브라우저에서 프록시 메서드를 사용하여 웹 서비스 메서드 또는 정적 페이지 메서드를 호출합니다. 자세한 내용은 웹 서비스를 클라이언트 스크립트로 노출을 참조하십시오.

그러나 다음과 같은 시나리오에서는 WebRequest 인스턴스를 만들고 사용할 수 있습니다.

  • 실행할 네트워크 요청이 간단한 웹 서비스 메서드 호출이 아닌 경우

  • HTTP 요청 속성을 직접 설정하려는 경우

  • Sys.Net.WebRequestExecutor 클래스를 기반으로 하는 사용자 지정 실행자를 사용하는 경우

예제

다음 예제에서는 WebRequest 클래스의 인스턴스를 만들어 GET 웹 요청을 실행하는 방법을 보여 줍니다. 이 예제에는 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 클래스