2012-07-13 4 views
1

서비스에 대한 웹 요청 전화를 할 때 왜 항상 새로운 세션 ID가 생성됩니까? 내가 sitea.comSession.SessionId ashx 요청 지속성

WebClient fs = new WebClient(); 
      var data = fs.DownloadData("http://siteb.com/serice.ashx"); 
      var tostring = System.Text.Encoding.ASCII.GetString(data); 
      return tostring; 

에서 호출하는 방법

이것은이 siteb.com의 서비스 코드

[WebMethod(EnableSession = true)] 
    private string Read(HttpContext context) 
    { 
     var value = context.Session.SessionId; 
     if (value !=null) return value.ToString(); 
      return "false"; 
    } 

값은 항상 모든 요청에 ​​대해 서로 다른입니다. 나는 이것을 어떻게 지속 할 수 있는가?

MSDN에서

답변

3

MSDN http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx에 완전한 세부 사항을 참조하십시오. 기본적으로 쿠키로 전송되지만 WebClient는 쿠키를 처리하지 않습니다. 당신은이 문제를 해결하기 위해 CookieAwareWebClient를 사용할 수 있습니다

public class CookieAwareWebClient : WebClient 
{ 
    private CookieContainer m_container = new CookieContainer(); 

    protected override WebRequest GetWebRequest(Uri address) 
    { 
     WebRequest request = base.GetWebRequest(address); 
     if (request is HttpWebRequest) 
     { 
      (request as HttpWebRequest).CookieContainer = m_container; 
     } 
     return request; 
    } 
} 

는만큼 당신이 웹 클라이언트의 동일한 인스턴스를 재사용하고, 당신이 동일한 세션 ID를 받아야합니다 (있는 경우 세션 과정 중 시간이 아닌 것이다).

+0

나를 위해 작동합니다. 아주 좋은 해결책! –

2

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

다음 코드 예제는 세션 상태를 사용하는 XML Web services의 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> 

당신은 세션 ID를 받아 후속 요청에 전달해야