다음을 통해 공유


HttpWebClientProtocol.CookieContainer 속성

정의

쿠키 컬렉션을 가져오거나 설정합니다.

public:
 property System::Net::CookieContainer ^ CookieContainer { System::Net::CookieContainer ^ get(); void set(System::Net::CookieContainer ^ value); };
public System.Net.CookieContainer CookieContainer { get; set; }
member this.CookieContainer : System.Net.CookieContainer with get, set
Public Property CookieContainer As CookieContainer

속성 값

CookieContainer 웹 서비스 클라이언트의 쿠키를 나타내는 A입니다.

예제

다음 코드 예제는 세션 상태를 사용하는 XML 웹 서비스의 Web Forms 클라이언트입니다. 클라이언트는 세션을 클라이언트의 세션 상태에 저장하여 세션을 고유하게 식별하는 HTTP 쿠키를 저장합니다.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<html>

    <script runat="server">

        void EnterBtn_Click(Object Src, EventArgs E) 
    {
      // Create a new instance of a proxy class for your XML Web service.
      ServerUsage su = new ServerUsage();
          CookieContainer cookieJar;

      // Check to see if the cookies have already been saved for this session.
      if (Session["CookieJar"] == null) 
        cookieJar= new CookieContainer();
          else
       cookieJar = (CookieContainer) Session["CookieJar"];

        // Assign the CookieContainer to the proxy class.
        su.CookieContainer = cookieJar;

      // Invoke an XML Web service method that uses session state and thus cookies.
      int count = su.PerSessionServiceUsage();         

      // Store the cookies received in the session state for future retrieval by this session.
      Session["CookieJar"] = cookieJar;

          // Populate the text box with the results from the call to the XML Web service method.
          SessionCount.Text = count.ToString();  
        }
         
    </script>
    <body>
       <form runat=server ID="Form1">
           
             Click to bump up the Session Counter.
             <p>
             <asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
             <p>
             <asp:label id="SessionCount"  runat=server/>
          
       </form>
    </body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<html>

    <script runat=server>

        Public Sub EnterBtn_Click(src As Object, E As EventArgs) 

      ' Create a new instance of a proxy class for your XML Web service.
      Dim su As ServerUsage = new ServerUsage()
          Dim cookieJar As CookieContainer

      ' Check to see if the cookies have already been saved for this session.
      If (Session("CookieJar") Is Nothing) 
        cookieJar= new CookieContainer()
          Else
       cookieJar = Session("CookieJar")
      End If
   

        ' Assign the CookieContainer to the proxy class.
        su.CookieContainer = cookieJar

      ' Invoke an XML Web service method that uses session state and thus cookies.
      Dim count As Integer = su.PerSessionServiceUsage()         

      ' Store the cookies received in the session state for future retrieval by this session.
      Session("CookieJar") = cookieJar

          ' Populate the text box with the results from the call to the XML Web service method.
          SessionCount.Text = count.ToString()  
    End Sub
         
    </script>
    <body>
       <form runat=server ID="Form1">
           
             Click to bump up the Session Counter.
             <p>
             <asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
             <p>
             <asp:label id="SessionCount"  runat=server/>
          
       </form>
    </body>
</html>

설명

XML 웹 서비스 메서드가 세션 상태를 사용하는 경우 해당 XML 웹 서비스 클라이언트에 대한 세션을 고유하게 식별하는 XML 웹 서비스 클라이언트에 응답 헤더에 쿠키가 다시 전달됩니다. XML 웹 서비스가 클라이언트에 대한 세션 상태를 유지하려면 클라이언트가 쿠키를 저장해야 합니다. 클라이언트는 XML 웹 서비스 메서드를 호출하기 전에 새 인스턴스 CookieContainer 를 만들고 프록시 클래스의 속성에 할당하여 CookieContainer HTTP 쿠키를 받습니다. 프록시 클래스 인스턴스가 범위를 벗어나는 경우 세션 상태를 유지해야 하는 경우 클라이언트는 XML 웹 서비스에 대한 호출 사이에 HTTP 쿠키를 저장해야 합니다. 예를 들어 Web Forms 클라이언트는 자체 세션 상태에 저장하여 HTTP 쿠키를 CookieContainer 저장할 수 있습니다. 모든 XML 웹 서비스에서 세션 상태를 사용하는 것은 아니므로 클라이언트가 항상 클라이언트 프록시의 속성을 사용할 CookieContainer 필요는 없으므로 XML 웹 서비스에 대한 설명서에서는 세션 상태가 사용되는지 여부를 명시해야 합니다.

적용 대상

추가 정보